當前位置: 首頁>>代碼示例>>C#>>正文


C# StandardKernel.Get方法代碼示例

本文整理匯總了C#中Ninject.Core.StandardKernel.Get方法的典型用法代碼示例。如果您正苦於以下問題:C# StandardKernel.Get方法的具體用法?C# StandardKernel.Get怎麽用?C# StandardKernel.Get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Ninject.Core.StandardKernel的用法示例。


在下文中一共展示了StandardKernel.Get方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: MemberBasedBindingOfTypeDependencies

		public void MemberBasedBindingOfTypeDependencies()
		{
			IModule module = new InlineModule(
				m => m.Bind(typeof(IMock)).To(typeof(ImplA)).Only(When.Context.Member.Tag.EqualTo("A")),
				m => m.Bind(typeof(IMock)).To(typeof(ImplB)).Only(When.Context.Member.Tag.EqualTo("B")),
				m => m.Bind(typeof(IMock)).To(typeof(ImplC)).Only(!When.Context.Member.Tag.EqualTo("A") & !When.Context.Member.Tag.EqualTo("B"))
			);

			using (var kernel = new StandardKernel(module))
			{
				var obj1 = kernel.Get<RequestsTagA>();
				Assert.That(obj1, Is.Not.Null);
				Assert.That(obj1.Child, Is.Not.Null);
				Assert.That(obj1.Child, Is.InstanceOfType(typeof(ImplA)));

				var obj2 = kernel.Get<RequestsTagB>();
				Assert.That(obj2, Is.Not.Null);
				Assert.That(obj2.Child, Is.Not.Null);
				Assert.That(obj2.Child, Is.InstanceOfType(typeof(ImplB)));

				var obj3 = kernel.Get<RequestsNoTag>();
				Assert.That(obj3, Is.Not.Null);
				Assert.That(obj3.Child, Is.Not.Null);
				Assert.That(obj3.Child, Is.InstanceOfType(typeof(ImplC)));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:26,代碼來源:ConditionalBindingFixture.cs

示例2: ManyPublishersOneSubscriber

		public void ManyPublishersOneSubscriber()
		{
			using (var kernel = new StandardKernel(new MessageBrokerModule()))
			{
				PublisherMock pub1 = kernel.Get<PublisherMock>();
				PublisherMock pub2 = kernel.Get<PublisherMock>();
				Assert.That(pub1, Is.Not.Null);
				Assert.That(pub2, Is.Not.Null);

				SubscriberMock sub = kernel.Get<SubscriberMock>();
				Assert.That(sub, Is.Not.Null);

				Assert.That(pub1.HasListeners);
				Assert.That(pub2.HasListeners);
				Assert.That(sub.LastMessage, Is.Null);

				pub1.SendMessage("Hello, world!");
				Assert.That(sub.LastMessage, Is.EqualTo("Hello, world!"));

				sub.LastMessage = null;
				Assert.That(sub.LastMessage, Is.Null);

				pub2.SendMessage("Hello, world!");
				Assert.That(sub.LastMessage, Is.EqualTo("Hello, world!"));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:26,代碼來源:MessageBrokerFixture.cs

示例3: OneInstanceCreatedForSingletonTypes

		public void OneInstanceCreatedForSingletonTypes()
		{
			using (var kernel = new StandardKernel())
			{
				var mock1 = kernel.Get<ObjectWithSingletonBehavior>();
				var mock2 = kernel.Get<ObjectWithSingletonBehavior>();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.SameAs(mock2));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:12,代碼來源:SingletonBehaviorFixture.cs

示例4: ManyInstancesCreatedForTransientTypes

		public void ManyInstancesCreatedForTransientTypes()
		{
			using (var kernel = new StandardKernel())
			{
				ObjectWithTransientBehavior mock1 = kernel.Get<ObjectWithTransientBehavior>();
				ObjectWithTransientBehavior mock2 = kernel.Get<ObjectWithTransientBehavior>();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.Not.SameAs(mock2));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:12,代碼來源:TransientBehaviorFixture.cs

示例5: BehaviorDefaultsToTransient

		public void BehaviorDefaultsToTransient()
		{
			using (var kernel = new StandardKernel())
			{
				SimpleObject mock1 = kernel.Get<SimpleObject>();
				SimpleObject mock2 = kernel.Get<SimpleObject>();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.Not.SameAs(mock2));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:12,代碼來源:TransientBehaviorFixture.cs

示例6: SameInstanceReusedOnSameThreadForOnePerThreadTypes

		public void SameInstanceReusedOnSameThreadForOnePerThreadTypes()
		{
			using (var kernel = new StandardKernel())
			{
				ObjectWithOnePerThreadBehavior mock1 = kernel.Get<ObjectWithOnePerThreadBehavior>();
				ObjectWithOnePerThreadBehavior mock2 = kernel.Get<ObjectWithOnePerThreadBehavior>();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.SameAs(mock2));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:12,代碼來源:OnePerThreadBehaviorFixture.cs

示例7: CustomBehaviorProviderUsed

		public void CustomBehaviorProviderUsed()
		{
			using (var kernel = new StandardKernel())
			{
				ObjectWithCustomBehavior mock1 = kernel.Get<ObjectWithCustomBehavior>();
				ObjectWithCustomBehavior mock2 = kernel.Get<ObjectWithCustomBehavior>();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.SameAs(mock2));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:12,代碼來源:CustomBehaviorFixture.cs

示例8: KernelLoadsModulesAndAppliesTheirBindings

		public void KernelLoadsModulesAndAppliesTheirBindings()
		{
			using (var kernel = new StandardKernel(new MockModuleA(), new MockModuleB()))
			{
				var one = kernel.Get<IServiceA>();
				Assert.That(one, Is.Not.Null);
				Assert.That(one, Is.InstanceOfType(typeof(ServiceImplA)));

				var two = kernel.Get<IServiceB>();
				Assert.That(two, Is.Not.Null);
				Assert.That(two, Is.InstanceOfType(typeof(ServiceImplB)));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:13,代碼來源:ModuleFixture.cs

示例9: InstancesOfTransietnServicesAreNotTracked

		public void InstancesOfTransietnServicesAreNotTracked()
		{
			using (var kernel = new StandardKernel())
			{
				var obj1 = kernel.Get<ObjectWithTransientBehavior>();
				Assert.That(obj1, Is.Not.Null);

				var obj2 = kernel.Get<ObjectWithTransientBehavior>();
				Assert.That(obj2, Is.Not.Null);

				var scope = kernel.Components.Tracker.GetScope(kernel);
				Assert.That(scope.Count, Is.EqualTo(0));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:14,代碼來源:TrackingFixture.cs

示例10: CanActivateGenericTypeViaGenericTypeDefinitionBindingWithSingletonBehavior

		public void CanActivateGenericTypeViaGenericTypeDefinitionBindingWithSingletonBehavior()
		{
			var module = new InlineModule(m => m.Bind(typeof(IGenericObject<>)).To(typeof(GenericImpl<>)).Using<SingletonBehavior>());

			using (var kernel = new StandardKernel(module))
			{
				var mock1 = kernel.Get<IGenericObject<string>>();
				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock1, Is.InstanceOfType(typeof(GenericImpl<string>)));

				var mock2 = kernel.Get<IGenericObject<int>>();
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock2, Is.InstanceOfType(typeof(GenericImpl<int>)));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:15,代碼來源:GenericProviderFixture.cs

示例11: ResultOfMethodCallWithSameArgumentsIsCached

		public void ResultOfMethodCallWithSameArgumentsIsCached()
		{
			IModule testModule = new InlineModule(m => m.Bind<CacheMock>().ToSelf());

			using (var kernel = new StandardKernel(new LinFuModule(), new CacheModule(), testModule))
			{
				var obj = kernel.Get<CacheMock>();

				CacheMock.ResetCounts();

				int result;

				result = obj.Multiply(2, 3);
				Assert.That(result, Is.EqualTo(6));
				Assert.That(CacheMock.MultiplyCount, Is.EqualTo(1));

				result = obj.Multiply(2, 3);
				Assert.That(result, Is.EqualTo(6));
				Assert.That(CacheMock.MultiplyCount, Is.EqualTo(1));

				result = obj.Multiply(4, 4);
				Assert.That(result, Is.EqualTo(16));
				Assert.That(CacheMock.MultiplyCount, Is.EqualTo(2));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:25,代碼來源:CacheFixture.cs

示例12: StaticInterceptorsAreRegisteredFromAttributesDefinedOnMethods

		public void StaticInterceptorsAreRegisteredFromAttributesDefinedOnMethods()
		{
			var module = new InlineModule(m => m.Bind<ObjectWithMethodInterceptor>().ToSelf());

			using (var kernel = new StandardKernel(module))
			{
				kernel.Components.Connect<IProxyFactory>(new DummyProxyFactory());

				var obj = kernel.Get<ObjectWithMethodInterceptor>();

				IContext context = kernel.Components.ContextFactory.Create(typeof(ObjectWithMethodInterceptor));

				IRequest request = new StandardRequest(
					context,
					obj,
					typeof(ObjectWithMethodInterceptor).GetMethod("Foo"),
					new object[0],
					Type.EmptyTypes
				);

				ICollection<IInterceptor> interceptors = kernel.Components.AdviceRegistry.GetInterceptors(request);
				IEnumerator<IInterceptor> enumerator = interceptors.GetEnumerator();
				enumerator.MoveNext();

				Assert.That(interceptors.Count, Is.EqualTo(1));
				Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:28,代碼來源:InterceptionFixture.cs

示例13: MemberBasedBindingOfTypeDependencies

		public void MemberBasedBindingOfTypeDependencies()
		{
			IModule module = new InlineModule(
				m => m.Bind(typeof(ObjectWithMethodInterceptor)).ToSelf(),
				m => m.Intercept(When.Request.Method.Name.StartsWith("F")).With<CountInterceptor>()
			);

			using (var kernel = new StandardKernel(module))
			{
				kernel.Components.Connect<IProxyFactory>(new DummyProxyFactory());

				var obj = kernel.Get<ObjectWithMethodInterceptor>();

				IContext context = kernel.Components.ContextFactory.Create(typeof(ObjectWithMethodInterceptor));

				IRequest request = new StandardRequest(
					context,
					obj,
					typeof(ObjectWithMethodInterceptor).GetMethod("Foo"),
					new object[0],
					Type.EmptyTypes
				);

				ICollection<IInterceptor> interceptors = kernel.Components.AdviceRegistry.GetInterceptors(request);
				Assert.That(interceptors.Count, Is.EqualTo(2));

				IEnumerator<IInterceptor> enumerator = interceptors.GetEnumerator();

				enumerator.MoveNext();
				Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));

				enumerator.MoveNext();
				Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:35,代碼來源:ConditionalInterceptionFixture.cs

示例14: CanCallParameterlessConstructor

		public void CanCallParameterlessConstructor()
		{
			using (var kernel = new StandardKernel())
			{
				var mock = kernel.Get<SimpleObject>();
				Assert.That(mock, Is.Not.Null);
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:8,代碼來源:StandardProviderFixture.cs

示例15: NewInstanceCreatedOnDifferentThreadForOnePerThreadTypes

		public void NewInstanceCreatedOnDifferentThreadForOnePerThreadTypes()
		{
			using (var kernel = new StandardKernel())
			{
				ObjectWithOnePerThreadBehavior mock1 = kernel.Get<ObjectWithOnePerThreadBehavior>();
				ObjectWithOnePerThreadBehavior mock2 = null;

				Thread thread = new Thread(delegate(object state) { mock2 = kernel.Get<ObjectWithOnePerThreadBehavior>(); });

				thread.Start();
				thread.Join();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.Not.SameAs(mock2));
			}
		}
開發者ID:jamarlthomas,項目名稱:ninject1,代碼行數:17,代碼來源:OnePerThreadBehaviorFixture.cs


注:本文中的Ninject.Core.StandardKernel.Get方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。