使用Ninject获取对象的实例是指在使用Ninject依赖注入框架时,如何创建和获取对象的实例。Ninject是一个开源的依赖注入框架,它可以帮助开发者更轻松地实现依赖注入,并且可以在不同的环境中使用。
在Ninject中,可以使用以下方法获取对象的实例:
kernel.Bind<IMyInterface>().ToMethod(context => new MyImplementation());
IMyInterface myInstance = new MyImplementation();
kernel.Bind<IMyInterface>().ToConstant(myInstance);
kernel.Bind<IMyInterface>().ToProvider<MyProvider>();
kernel.Bind<MyImplementation>().ToSelf();
kernel.Bind<IMyInterface>().To<MyImplementation>().InSingletonScope();
kernel.Bind<IMyInterface>().To<MyImplementation>().InTransientScope();
kernel.Bind<IMyInterface>().To<MyImplementation>().InRequestScope();
kernel.Bind<IMyInterface>().To<MyImplementation>().InThreadScope();
kernel.Bind<IMyInterface>().To<MyImplementation>().InNamedScope("myScope");
kernel.Bind<IMyInterface>().To<MyImplementation>().WhenInjectedInto<MyConsumer>();
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgument("arg1", "value1");
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValue("PropertyName", "PropertyValue");
kernel.Bind<IMyInterface>().ToMethod(context => new MyImplementation(context.Parameters.First()));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithCircularDependencies();
kernel.Bind<IMyInterface>().To<MyImplementation>().OnActivation(instance => instance.Initialize());
kernel.Bind<IMyInterface>().To<MyImplementation>().OnDeactivation(instance => instance.Dispose());
kernel.Bind<IMyInterface>().To<MyImplementation>().When(request => request.Target != null && request.Target.Member.Name == "MyMethod");
kernel.Bind<IMyInterface>().To<MyImplementation>().WithMetadata("key", "value");
kernel.Bind<IMyInterface>().To<MyImplementation>().WithAnyAttribute();
kernel.Bind<IMyInterface>().To<MyImplementation>().WithAttribute(new MyAttribute());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithAttributes(new MyAttribute(), new MyAttribute2());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithKernel(new MyKernel());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParentScope();
kernel.Bind<IMyInterface>().To<MyImplementation>().WithScope(new MyScope());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithUniqueId("myUniqueId");
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromContext("arg1", context => context.Parameters.First());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromContext("PropertyName", context => context.Parameters.First());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromContext(context => new ConstructorArgument("arg1", context.Parameters.First()));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromMethod("arg1", context => context.Parameters.First());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromMethod("PropertyName", context => context.Parameters.First());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromMethod(context => new ConstructorArgument("arg1", context.Parameters.First()));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromProvider("arg1", context => new MyProvider());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromProvider("PropertyName", context => new MyProvider());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromProvider(context => new MyProvider());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromResolutionRoot("arg1", context => context.Kernel.Get<MyService>());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromResolutionRoot("PropertyName", context => context.Kernel.Get<MyService>());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromResolutionRoot(context => new ConstructorArgument("arg1", context.Kernel.Get<MyService>()));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromService("arg1", context => context.Kernel.Get<MyService>());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromService("PropertyName", context => context.Kernel.Get<MyService>());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromService(context => new ConstructorArgument("arg1", context.Kernel.Get<MyService>()));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromSyntax("arg1", context => context.Kernel.Get<MyService>());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromSyntax("PropertyName", context => context.Kernel.Get<MyService>());
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromSyntax(context => new ConstructorArgument("arg1", context.Kernel.Get<MyService>()));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromType("arg1", typeof(MyService));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromType("PropertyName", typeof(MyService));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromType(typeof(MyService));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromValue("arg1", "value1");
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromValue("PropertyName", "value1");
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromValue(new ConstructorArgument("arg1", "value1"));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromValues("arg1", new[] { "value1", "value2" });
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromValues("PropertyName", new[] { "value1", "value2" });
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromValues(new ConstructorArgument("arg1", new[] { "value1", "value2" }));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromName("arg1", "name1");
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromName("PropertyName", "name1");
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromName(new ConstructorArgument("arg1", "name1"));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromTypeOf("arg1", typeof(MyService));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromTypeOf("PropertyName", typeof(MyService));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromTypeOf(typeof(MyService));
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromTypeOfName("arg1", typeof(MyService), "name1");
kernel
领取专属 10元无门槛券
手把手带您无忧上云