本文整理汇总了C#中Container.Resolve方法的典型用法代码示例。如果您正苦于以下问题:C# Container.Resolve方法的具体用法?C# Container.Resolve怎么用?C# Container.Resolve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container.Resolve方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test_Multiple2
public void Test_Multiple2()
{
var container = new Container(log: Write);
container.RegisterSingleton<Foo>();
container.RegisterSingleton<IFoo1>();
Assert.Equal(container.Resolve<Foo>(), container.Resolve<IFoo1>());
}
示例2: RegisterResolveArguments
public void RegisterResolveArguments()
{
var c = new Container();
c.Register<ITest1>(() => new Test1());
var t1 = c.Resolve<ITest1>();
Assert.IsNotNull(t1);
Assert.IsInstanceOfType(typeof(Test1), t1);
c.Register<ITest2>(() => new Test2());
var t2 = c.Resolve<ITest2>();
Assert.IsNotNull(t2);
Assert.IsInstanceOfType(typeof(Test2), t2);
c.Register<ITest3, ITest1, ITest2>((a1, a2) => new Test3(a1, a2));
var t3 = c.Resolve<ITest3>();
Assert.IsNotNull(t3);
Assert.IsInstanceOfType(typeof(Test3), t3);
Assert.IsNotNull(t3.Test1);
Assert.IsInstanceOfType(typeof(Test1), t3.Test1);
Assert.IsNotNull(t3.Test2);
Assert.IsInstanceOfType(typeof(Test2), t3.Test2);
}
示例3: T03_Registration_Concrete_Multiple
public void T03_Registration_Concrete_Multiple()
{
var container = new Container(log: Write);
container.RegisterSingleton<IMarker1, ClassA1>();
container.Resolve<IMarker1>();
Assert.Throws<TypeAccessException>(() => container.Resolve<ClassA1>()).WriteMessageTo(Write);
}
示例4: Test_Recursive_Dependency
public void Test_Recursive_Dependency()
{
var container = new Container(DefaultLifestyle.Singleton, log: Write);
Assert.Throws<TypeAccessException>(() => container.Resolve<Class1>()).WriteMessageTo(Write);
Assert.Throws<TypeAccessException>(() => container.Resolve<Class2>()).WriteMessageTo(Write);
Assert.Throws<TypeAccessException>(() => container.Resolve<Class3>()).WriteMessageTo(Write);
}
示例5: InterfaceReRegisteredFromClassToObjectFactory_Success
public void InterfaceReRegisteredFromClassToObjectFactory_Success()
{
var c = new Container();
c.RegisterType<IEmptyClass, EmptyClass>().AsPerThread();
IEmptyClass emptyClass1 = null;
IEmptyClass emptyClass2 = null;
IEmptyClass emptyClass3 = null;
IEmptyClass emptyClass4 = null;
var thread = new Thread(() =>
{
emptyClass1 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);
emptyClass2 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);
c.RegisterType<IEmptyClass>(() => new EmptyClass()).AsPerThread();
emptyClass3 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);
emptyClass4 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);
});
thread.Start();
thread.Join();
Assert.AreEqual(emptyClass1, emptyClass2);
Assert.AreEqual(emptyClass3, emptyClass4);
Assert.AreNotEqual(emptyClass1, emptyClass3);
}
示例6: Test_Cannot_Create_Dependency
public void Test_Cannot_Create_Dependency()
{
var container = new Container(DefaultLifestyle.Singleton, log: Write);
Assert.Throws<TypeAccessException>(() => container.Resolve<ClassZ>()).WriteMessageTo(Write);
Assert.Throws<TypeAccessException>(() => container.Resolve<ClassY>()).WriteMessageTo(Write);
Assert.Throws<TypeAccessException>(() => container.Resolve<ClassX>()).WriteMessageTo(Write);
}
示例7: InterfaceReRegisteredFromInstanceToInstance_Success
public void InterfaceReRegisteredFromInstanceToInstance_Success()
{
var c = new Container();
IEmptyClass emptyClass = new EmptyClass();
IEmptyClass emptyClass3 = new EmptyClass();
c.RegisterInstance(emptyClass).AsPerThread();
IEmptyClass emptyClass1 = null;
IEmptyClass emptyClass2 = null;
IEmptyClass emptyClass4 = null;
IEmptyClass emptyClass5 = null;
var thread = new Thread(() =>
{
emptyClass1 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);
emptyClass2 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);
c.RegisterInstance(emptyClass3).AsPerThread();
emptyClass4 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);
emptyClass5 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);
});
thread.Start();
thread.Join();
Assert.AreEqual(emptyClass, emptyClass1);
Assert.AreEqual(emptyClass1, emptyClass2);
Assert.AreEqual(emptyClass3, emptyClass4);
Assert.AreEqual(emptyClass4, emptyClass5);
Assert.AreNotEqual(emptyClass, emptyClass3);
}
示例8: Initialize
public void Initialize(HttpConfiguration configuration, Pandora pandora)
{
var container = new Container();
container.RegisterSingleton<Pandora>(() => pandora);
Func<IPipelineTransport> transport = () => container.Resolve<IPipelineTransport>();
Func<ISerializer> serializer = () => container.Resolve<ISerializer>();
container.RegisterSingleton<IPublisher<ICommand>>(() => new PipelinePublisher<ICommand>(transport(), serializer()));
var serviceLocator = new ServiceLocator(container);
ControllerFactory = new ServiceLocatorFactory(serviceLocator);
var cfg = new CronusSettings(container)
.UseCluster(cluster =>
cluster.UseAggregateRootAtomicAction(atomic =>
{
if (pandora.Get<bool>("enable_redis_atomic_action"))
atomic.UseRedis(redis => redis.SetLockEndPoints(pandora.Get("redis_endpoints").ParseIPEndPoints(";")));
else
atomic.WithInMemory();
}))
.UseContractsFromAssemblies(new[] { Assembly.GetAssembly(typeof(PushNotificationWasSent)) })
.UseRabbitMqTransport(x =>
{
x.Server = pandora.Get("rabbitmq_server");
x.Username = pandora.Get("rabbitmq_username");
x.Password = pandora.Get("rabbitmq_password");
x.VirtualHost = pandora.Get("rabbitmq_virtualhost");
});
(cfg as ICronusSettings).Build();
}
示例9: Container
public void DifferentThreads_DifferentObjects_RegisterClassWithDependencyPropertyAndDependencyMethodWithSameType_Success()
{
var c = new Container();
c.RegisterType<EmptyClass>().AsPerThread();
c.RegisterType<SampleClassWithClassDependencyPropertyAndDependencyMethodWithSameType>();
SampleClassWithClassDependencyPropertyAndDependencyMethodWithSameType sampleClass1 = null;
SampleClassWithClassDependencyPropertyAndDependencyMethodWithSameType sampleClass2 = null;
var thread1 = new Thread(() => { sampleClass1 = c.Resolve<SampleClassWithClassDependencyPropertyAndDependencyMethodWithSameType>(ResolveKind.PartialEmitFunction); });
thread1.Start();
thread1.Join();
var thread2 = new Thread(() => { sampleClass2 = c.Resolve<SampleClassWithClassDependencyPropertyAndDependencyMethodWithSameType>(ResolveKind.PartialEmitFunction); });
thread2.Start();
thread2.Join();
Assert.IsNotNull(sampleClass1.EmptyClassFromDependencyProperty);
Assert.IsNotNull(sampleClass1.EmptyClassFromDependencyMethod);
Assert.AreEqual(sampleClass1.EmptyClassFromDependencyProperty, sampleClass1.EmptyClassFromDependencyMethod);
Assert.IsNotNull(sampleClass2.EmptyClassFromDependencyProperty);
Assert.IsNotNull(sampleClass2.EmptyClassFromDependencyMethod);
Assert.AreEqual(sampleClass2.EmptyClassFromDependencyProperty, sampleClass2.EmptyClassFromDependencyMethod);
Assert.AreNotEqual(sampleClass1, sampleClass2);
Assert.AreNotEqual(sampleClass1.EmptyClassFromDependencyProperty, sampleClass2.EmptyClassFromDependencyProperty);
Assert.AreNotEqual(sampleClass1.EmptyClassFromDependencyMethod, sampleClass2.EmptyClassFromDependencyMethod);
}
开发者ID:amularczyk,项目名称:NiquIoC,代码行数:25,代码来源:RegisterClassWithDependencyPropertyAndDependencyMethodTests.cs
示例10: CachedLifetimeManagerReturnsDifferentObjectIfAbsoluteTimeExpired
public void CachedLifetimeManagerReturnsDifferentObjectIfAbsoluteTimeExpired()
{
var cachedltm = new CachedLifetime()
.ExpiresOn(DateTime.UtcNow.AddSeconds(2));
var container = new Container();
var ireg = container.Register<IFoo>(c => new Foo1())
.WithLifetimeManager(cachedltm);
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IFoo>();
// simulate expiry
Thread.Sleep(3000);
var result3 = container.Resolve<IFoo>();
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsNotNull(result3);
Assert.AreSame(result1, result2); // cache not expired
Assert.AreNotSame(result2, result3); // cache expired
}
示例11: DifferentObjects_RegisterInterfaceWithManyInterfaceDependencyProperties_Success
public void DifferentObjects_RegisterInterfaceWithManyInterfaceDependencyProperties_Success()
{
var c = new Container();
c.RegisterType<IEmptyClass, EmptyClass>();
c.RegisterType<ISampleClassWithInterfaceAsParameter, SampleClassWithInterfaceAsParameter>().AsSingleton();
c.RegisterType<ISampleClassWithManyInterfaceDependencyProperties, SampleClassWithManyInterfaceDependencyProperties>();
var sampleClass1 = c.Resolve<ISampleClassWithManyInterfaceDependencyProperties>(ResolveKind.PartialEmitFunction);
var sampleClass2 = c.Resolve<ISampleClassWithManyInterfaceDependencyProperties>(ResolveKind.PartialEmitFunction);
Assert.IsNotNull(sampleClass1.EmptyClass);
Assert.IsNotNull(sampleClass1.SampleClass);
Assert.IsNotNull(sampleClass1.SampleClass.EmptyClass);
Assert.AreNotEqual(sampleClass1.SampleClass.EmptyClass, sampleClass1.EmptyClass);
Assert.IsNotNull(sampleClass2.EmptyClass);
Assert.IsNotNull(sampleClass2.SampleClass);
Assert.IsNotNull(sampleClass2.SampleClass.EmptyClass);
Assert.AreNotEqual(sampleClass2.SampleClass.EmptyClass, sampleClass2.EmptyClass);
Assert.AreNotEqual(sampleClass1, sampleClass2);
Assert.AreNotEqual(sampleClass1.EmptyClass, sampleClass2.EmptyClass);
Assert.AreEqual(sampleClass1.SampleClass, sampleClass2.SampleClass);
Assert.AreEqual(sampleClass1.SampleClass.EmptyClass, sampleClass2.SampleClass.EmptyClass);
}
示例12: Container
public void DifferentThreads_DifferentObjects_RegisterInterfaceWithDependencyPropertyAndDependencyMethodWithDifferentTypes_Success()
{
var c = new Container();
c.RegisterType<IEmptyClass, EmptyClass>().AsPerThread();
c.RegisterType<ISampleClassWithInterfaceAsParameter, SampleClassWithInterfaceAsParameter>().AsPerThread();
c.RegisterType<ISampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithDifferentTypes, SampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithDifferentTypes>();
ISampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithDifferentTypes sampleClass1 = null;
ISampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithDifferentTypes sampleClass2 = null;
var thread1 = new Thread(() => { sampleClass1 = c.Resolve<ISampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithDifferentTypes>(ResolveKind.PartialEmitFunction); });
thread1.Start();
thread1.Join();
var thread2 = new Thread(() => { sampleClass2 = c.Resolve<ISampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithDifferentTypes>(ResolveKind.PartialEmitFunction); });
thread2.Start();
thread2.Join();
Assert.IsNotNull(sampleClass1.SampleClass);
Assert.IsNotNull(sampleClass1.EmptyClass);
Assert.AreNotEqual(sampleClass1.SampleClass, sampleClass1.EmptyClass);
Assert.AreEqual(sampleClass1.SampleClass.EmptyClass, sampleClass1.EmptyClass);
Assert.IsNotNull(sampleClass2.SampleClass);
Assert.IsNotNull(sampleClass2.EmptyClass);
Assert.AreNotEqual(sampleClass2.SampleClass, sampleClass2.EmptyClass);
Assert.AreEqual(sampleClass2.SampleClass.EmptyClass, sampleClass2.EmptyClass);
Assert.AreNotEqual(sampleClass1, sampleClass2);
Assert.AreNotEqual(sampleClass1.SampleClass, sampleClass2.SampleClass);
Assert.AreNotEqual(sampleClass1.EmptyClass, sampleClass2.EmptyClass);
}
开发者ID:amularczyk,项目名称:NiquIoC,代码行数:28,代码来源:RegisterInterfaceWithDependencyPropertyAndDependencyMethodTests.cs
示例13: ClassReRegisteredFromClassToTheSameClass_Success
public void ClassReRegisteredFromClassToTheSameClass_Success()
{
var c = new Container();
c.RegisterType<EmptyClass>().AsPerThread();
EmptyClass emptyClass1 = null;
EmptyClass emptyClass2 = null;
EmptyClass emptyClass3 = null;
EmptyClass emptyClass4 = null;
var thread = new Thread(() =>
{
emptyClass1 = c.Resolve<EmptyClass>(ResolveKind.PartialEmitFunction);
emptyClass2 = c.Resolve<EmptyClass>(ResolveKind.PartialEmitFunction);
c.RegisterType<EmptyClass>().AsPerThread();
emptyClass3 = c.Resolve<EmptyClass>(ResolveKind.PartialEmitFunction);
emptyClass4 = c.Resolve<EmptyClass>(ResolveKind.PartialEmitFunction);
});
thread.Start();
thread.Join();
Assert.AreEqual(emptyClass1, emptyClass2);
Assert.AreEqual(emptyClass3, emptyClass4);
Assert.AreNotEqual(emptyClass1, emptyClass3);
}
示例14: DifferentObjects_RegisterClassWithManyClassParametersInDependencyMethod_Success
public void DifferentObjects_RegisterClassWithManyClassParametersInDependencyMethod_Success()
{
var c = new Container();
c.RegisterType<EmptyClass>();
c.RegisterType<SampleClass>().AsSingleton();
c.RegisterType<SampleClassWithManyClassParametersInDependencyMethod>();
var sampleClass1 = c.Resolve<SampleClassWithManyClassParametersInDependencyMethod>(ResolveKind.PartialEmitFunction);
var sampleClass2 = c.Resolve<SampleClassWithManyClassParametersInDependencyMethod>(ResolveKind.PartialEmitFunction);
Assert.IsNotNull(sampleClass1.EmptyClass);
Assert.IsNotNull(sampleClass1.SampleClass);
Assert.IsNotNull(sampleClass1.SampleClass.EmptyClass);
Assert.AreNotEqual(sampleClass1.SampleClass.EmptyClass, sampleClass1.EmptyClass);
Assert.IsNotNull(sampleClass2.EmptyClass);
Assert.IsNotNull(sampleClass2.SampleClass);
Assert.IsNotNull(sampleClass2.SampleClass.EmptyClass);
Assert.AreNotEqual(sampleClass2.SampleClass.EmptyClass, sampleClass2.EmptyClass);
Assert.AreNotEqual(sampleClass1, sampleClass2);
Assert.AreNotEqual(sampleClass1.EmptyClass, sampleClass2.EmptyClass);
Assert.AreEqual(sampleClass1.SampleClass, sampleClass2.SampleClass);
Assert.AreEqual(sampleClass1.SampleClass.EmptyClass, sampleClass2.SampleClass.EmptyClass);
}
示例15: T03_Concrete_Interface
public void T03_Concrete_Interface()
{
var container = new Container(log: Write);
container.RegisterSingleton<ISomeClass>();
container.RegisterSingleton<SomeClass>();
Assert.Equal(container.Resolve<SomeClass>(), container.Resolve<ISomeClass>());
}