本文整理汇总了C#中Container.As方法的典型用法代码示例。如果您正苦于以下问题:C# Container.As方法的具体用法?C# Container.As怎么用?C# Container.As使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container.As方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AsAndAsUseTest
public void AsAndAsUseTest()
{
using (var ioc = new Container())
{
ioc.As<ITestDisposable>().AndAs<IDemoInterface>().Use<MockDisposableClass>(ThreadScope.Instance);
var instance1 = ioc.GetService<ITestDisposable>();
var someInstance = ioc.GetService<IDemoInterface>();
Assert.AreSame(instance1, someInstance);
}
}
示例2: SimpleEventTest
public void SimpleEventTest()
{
using (var ioc = new Container())
{
IConfiguredFactory<MockDisposableClass> configuredFactory
= ioc.As<ITestDisposable>().Use<MockDisposableClass>(ThreadScope.Instance);
var created = false;
configuredFactory.AfterCreate += (sender, args) =>
{
Console.Write("Instance created!");
created = true;
};
var instance1 = ioc.GetService<ITestDisposable>();
Assert.IsTrue(created);
}
}
示例3: DisposeRegistration
public void DisposeRegistration()
{
var disposed = false;
var created = false;
using (var ioc = new Container())
{
IConfiguredFactory<MockDisposableClass> registration = ioc.As<ITestDisposable>().Use<MockDisposableClass>(ioc);
registration.AfterCreate += (sender, args) =>
{
args.Component.BeforeDisposeAction =() => disposed = true;
//((AfterCreateEventArgs)args).Component = new MockDisposableClass2();
};
var myComponent = ioc.GetService<ITestDisposable>();
registration.Dispose();
Assert.IsTrue(disposed);
Assert.IsFalse(ioc.IsConfiguredFor(typeof(ITestDisposable)));
}
}
示例4: RegisterInstanceNotExternallyOwnedTest
public void RegisterInstanceNotExternallyOwnedTest() //todo: tests with opened generics types
{
var anyInstance = new MockDisposableClass();
using (var ioc = new Container())
{
ioc.As<ITestDisposable>().Use(anyInstance, externallyOwned: false);
ioc.GetService<ITestDisposable>();//creating isntance
}
Assert.AreEqual(true, anyInstance.Disposed);//instance is not externally owned -> dispose has been called
}
示例5: RegisterInstanceScopedTest
[Ignore] //This feature is not implemented yet.
public void RegisterInstanceScopedTest() //todo: tests with opened generics types
{
var anyInstance = new MockDisposableClass();
using (var ioc = new Container())
{
ioc.As<ITestDisposable>().Use(anyInstance,true,ThreadScope.Instance); //just for current thread
Task.Factory.StartNew(() =>
{
var myInstance = ioc.GetService<ITestDisposable>();
Assert.IsNull(myInstance); //should be null, because this is another thread
}).Wait();
}
}
示例6: RegisterInstanceTest
public void RegisterInstanceTest() //todo: tests with opened generics types
{
var anyInstance = new MockDisposableClass();
using (var ioc = new Container())
{
ioc.As<ITestDisposable>().Use(anyInstance);
var myInstance = ioc.GetService<ITestDisposable>(); //creating isntance
Assert.AreSame(anyInstance,myInstance); //should be some instance
}
}
示例7: MultiThreadTest
public void MultiThreadTest()
{
using (var ioc = new Container())
{
ioc.As<LongTimeConstructedClass>().Use<LongTimeConstructedClass>(ioc);
List<Task> all = new List<Task>();
for (int n = 0;n<100;n++)
{
Task t = new Task(()=>ioc.GetService<LongTimeConstructedClass>());
all.Add(t);
t.Start();
}
all.ForEach(t=>t.Wait());
}
Assert.AreEqual(1,LongTimeConstructedClass.Counter);
}
示例8: Advanced1bTest
public virtual void Advanced1bTest()
{
using (var ioc = new Container())
{
//tvorba listu vyzaduje v konstruktoru číslo (kapacita listu)
ioc.As(typeof (int)).Use(() => 5);
ioc.As(typeof (IList<>)).Use(typeof (List<>),ioc/*singleton*/);
Assert.AreEqual(5, ioc.GetService<int>()); //jen pro kontrolu
var f1 = (Func<IList<string>>) ioc.GetService(typeof (Func<IList<string>>));
var f2 = (Func<IList<string>>) ioc.GetService(typeof (Func<IList<string>>));
//protoze IList je definovan jako singleton, tak list vraceny pomocí teto fce musí byt vzdy stejný
Assert.AreSame(f1(), f2());
//a dokonce by to mela byt i stejná fce
Assert.AreSame(f1, f2);
var l1 = ioc.GetService<Lazy<IList<string>>>();
var l2 = ioc.GetService<Lazy<IList<string>>>();
//protoze IList je definovan jako singleton, tak list vraceny pomocí teto fce musí byt vzdy stejný
Assert.AreSame(l1.Value, l2.Value);
//a dokonce by to mela byt i stejné instance
Assert.AreSame(l1, l2);
var list = ioc.GetService<IList<string>>();
//jednoduse vraceny list musí byt stejný jako vraceny pomocí Lazy
Assert.AreSame(list, l1.Value);
//jednoduse vraceny list musí byt stejný jako list vraceny pomoci funkce
Assert.AreSame(list, f1());
}
}
示例9: EventTest
public void EventTest()
{
byte released = 0;
byte created = 0;
using (var ioc = new Container())
{
//todo: zavest IConfiguredPluginGroup<TCommonType>
var expr = ioc.As<ITestDisposable>().AndAs<IDemoInterface>().AndAs<IDisposable>();
IConfiguredFactory<MockDisposableClass> regInfo = expr.Use<MockDisposableClass>(ioc
/*=singleton*/);
regInfo.AfterCreate += (sender, args) =>
{
created++; //must be called only once!
};
regInfo.BeforeRelease += (sender, args) =>
{
released++; //must be called only once!
args.RunDispose = true; //it is by default, only for demonstration.
};
var myComponent1 = ioc.GetService<IDemoInterface>();
var myComponent2 = ioc.GetService<ITestDisposable>();
var myComponent3 = ioc.GetService<IDisposable>();
Assert.AreSame(myComponent1, myComponent2);
Assert.AreSame(myComponent1, myComponent3);
Assert.IsTrue(created == 1); //was called only once!
}
Assert.IsTrue(released == 1);//was called only once!
}
示例10: Dispose1Test
public virtual void Dispose1Test()
{
bool disposed = false;
ITestDisposable a;
using (var ioc = new Container())
{
var registration = ioc.As<ITestDisposable>().Use<MockDisposableClass>(ioc); //as singleton /* AccordConatiner implements IPersunScope*/
a = ioc.GetService<ITestDisposable>();
a.BeforeDisposeAction =
delegate
{
disposed = true;
};
}
Assert.IsTrue(disposed);
}
示例11: DisposeServiceTest
public virtual void DisposeServiceTest()
{
bool disposed = false;
using (var ioc = new Container())
{
ioc.As<ITestDisposable>().Use<MockDisposableClass>(ioc); //as singleton /* AccordConatiner implements IPersunScope*/
var a = ioc.GetService<ITestDisposable>();
a.BeforeDisposeAction =
delegate
{
disposed = true;
};
Assert.IsTrue(ioc.IsConfiguredFor(typeof(ITestDisposable)));
ioc.DisposeService(typeof(ITestDisposable));
Assert.IsTrue(disposed);
Assert.IsFalse(ioc.IsConfiguredFor(typeof(ITestDisposable)));
}
}
示例12: OpenGeneric_Shared_Test
public void OpenGeneric_Shared_Test()
{
using (var ioc = new Container())
{
ioc.ConfigurationManager.SupportForIEnumerable = false;//protoze predefinováváme IEnumerable<>
ioc.As<int>().Use(() => 2); //list potrebuje pocet polozek
ioc.As(typeof(IList<>)).AndAs(typeof(IEnumerable<>)).Use(typeof(List<>),ioc/*=singleton*/);
var a = ioc.GetService<IList<string>>();
Assert.IsNotNull(a);
var b = ioc.GetService<IEnumerable<string>>();
Assert.AreSame(a,b);
}
}
示例13: OpenGeneric_Events_Test
public void OpenGeneric_Events_Test()
{
bool released = false;
using (var ioc = new Container())
{
bool created = false;
IConfiguredFactory regInfo =
ioc.As(typeof (IList<>)).Use(typeof (List<>).GetConstructor(Type.EmptyTypes), ioc /*=singleton*/);
regInfo.AfterCreate += (sender, args) =>
{
created = true;
};
regInfo.BeforeRelease += (sender, args) =>
{
released = true;
};
var list = ioc.GetService<IList<string>>();
Assert.AreEqual(true, created);
Assert.AreEqual(false, released);
}
Assert.AreEqual(true, released);
}
示例14: OpenGeneric_Simple_Test
public void OpenGeneric_Simple_Test()
{
using (var ioc = new Container())
{
IConfiguredFactory regInfo = ioc.As(typeof(IList<>)).Use(typeof(List<>).GetConstructor(Type.EmptyTypes));
var list = ioc.GetService<IList<string>>();
Assert.IsNotNull(list);
}
}
示例15: UsingConstructorForOpenGenericTest
public void UsingConstructorForOpenGenericTest()
{
using (var ioc = new Container())
{
ioc.As(typeof(IList<>)).Use(typeof(List<>).GetConstructor(new Type[] {}));//we are using empty constructor for List<>
var list = ioc.GetService<IList<string>>();
Assert.IsNotNull(list);
}
}