本文整理汇总了C#中System.IocContainer.GetScope方法的典型用法代码示例。如果您正苦于以下问题:C# IocContainer.GetScope方法的具体用法?C# IocContainer.GetScope怎么用?C# IocContainer.GetScope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IocContainer
的用法示例。
在下文中一共展示了IocContainer.GetScope方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ContainerScopeTryResolveWorks
public void ContainerScopeTryResolveWorks()
{
using (var container = new IocContainer())
{
// Register
container.Register<IFoo, Foo1>();
container.Register<IFoo, Foo2>("Key1");
container.Register<IBar, Bar1>();
using (var scope = container.GetScope())
{
IFoo instance1, instance2;
var result1 = scope.TryResolve<IFoo>(out instance1);
var result2 = scope.TryResolve<IFoo>(out instance2);
Assert.AreSame(instance1, instance2);
IFooBar instance3, instance4;
var result3 = scope.TryResolve<IFooBar>(out instance3);
var result4 = scope.TryResolve<IFooBar>(out instance4);
Assert.AreSame(instance3, instance4);
}
}
}
示例2: ContainerScopeResolveAllWorks
public void ContainerScopeResolveAllWorks()
{
using (var container = new IocContainer())
{
// Register
container.Register<IFoo, Foo1>();
container.Register<IFoo, Foo2>("Key1");
container.Register<IBar, Bar1>();
using (var scope = container.GetScope())
{
var all = scope.ResolveAll<IFoo>().ToList();
Assert.IsTrue(all.Count == 2);
Assert.IsInstanceOfType(all[0], typeof(Foo1));
Assert.IsInstanceOfType(all[1], typeof(Foo2));
// Scope
var all2 = scope.ResolveAll<IFoo>().ToList();
Assert.AreSame(all[0], all2[0]);
Assert.AreSame(all[1], all2[1]);
using (var scope2 = container.GetScope())
{
var all3 = scope2.ResolveAll<IFoo>().ToList();
Assert.IsTrue(all3.Count == 2);
Assert.IsInstanceOfType(all[0], typeof(Foo1));
Assert.IsInstanceOfType(all[1], typeof(Foo2));
Assert.AreNotSame(all[0], all3[0]);
Assert.AreNotSame(all[1], all3[1]);
}
}
}
}
示例3: ContainerScopeResolveWorks
public void ContainerScopeResolveWorks()
{
using (var container = new IocContainer())
{
// Register
container.Register<IFoo, Foo1>();
container.Register<IFoo, Foo2>("Key1");
container.Register<IBar, Bar1>();
using (var scope = container.GetScope())
{
var instance1 = scope.Resolve<IFoo>();
var instance2 = scope.Resolve<IFoo>();
Assert.AreSame(instance1, instance2);
}
}
}