本文整理汇总了C#中ServiceContainer.GetAllInstances方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceContainer.GetAllInstances方法的具体用法?C# ServiceContainer.GetAllInstances怎么用?C# ServiceContainer.GetAllInstances使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceContainer
的用法示例。
在下文中一共展示了ServiceContainer.GetAllInstances方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldHandleMoreThanNineImplementationsForSameInterface
public void ShouldHandleMoreThanNineImplementationsForSameInterface()
{
var container = new ServiceContainer();
container.Register<Foo0>();
container.Register<Foo1>();
container.Register<Foo2>();
container.Register<Foo3>();
container.Register<Foo4>();
container.Register<Foo5>();
container.Register<Foo6>();
container.Register<Foo7>();
container.Register<Foo8>();
// Causes System.ArgumentException in
// ILGenerator.Emit(OpCode code, Type type)
// when creating an ArrayAccess expression
// using a non-int32 type as index (in this
// case, an sbyte)
container.Register<Foo9>();
var allInstances = container.GetAllInstances<IFoo>().ToList();
Assert.Equal(10, allInstances.Count);
}
示例2: ShouldRaiseActivationExceptionIfSomethingGoesWrong2
public void ShouldRaiseActivationExceptionIfSomethingGoesWrong2()
{
var ioc = new ServiceContainer();
ioc.Register(typeof(IService1), "NamedService1", c => { throw new Exception("Fail"); });
ioc.GetAllInstances<IService1>();
}
示例3: GetAllInstances
public void GetAllInstances()
{
var ioc = new ServiceContainer();
Assert.AreEqual(0, ioc.GetAllInstances<IService1>().Count());
ioc.Register(typeof(IService1), null, typeof(Service1));
ioc.Register(typeof(IService1), "NamedService1", typeof(Service1));
ioc.Register(typeof(IService1), "NamedService2", typeof(Service1));
Assert.AreEqual(2, ioc.GetAllInstances<IService1>().Count());
Assert.IsNotNull(ioc.GetAllInstances<IService1>().ToArray()[0]);
Assert.IsNotNull(ioc.GetAllInstances<IService1>().ToArray()[1]);
}