本文整理汇总了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)));
}
}
示例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!"));
}
}
示例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));
}
}
示例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));
}
}
示例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));
}
}
示例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));
}
}
示例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));
}
}
示例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)));
}
}
示例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));
}
}
示例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>)));
}
}
示例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));
}
}
示例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)));
}
}
示例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)));
}
}
示例14: CanCallParameterlessConstructor
public void CanCallParameterlessConstructor()
{
using (var kernel = new StandardKernel())
{
var mock = kernel.Get<SimpleObject>();
Assert.That(mock, Is.Not.Null);
}
}
示例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));
}
}