鑑於你的榜樣,你不需要做什麼特別的事情不管你是明確註冊類型:
container.Register(Component.For(typeof(IInterface<Cat>)).ImplementedBy(typeof(CatImplementer)));
container.Register(Component.For(typeof(IInterface<>)).ImplementedBy(typeof(Fallback<>)));
或含蓄:
在這兩種情況下,下面的代碼:
IInterface<Cat> cat = container.Resolve<IInterface<Cat>>();
IInterface<Dog> dog = container.Resolve<IInterface<Dog>>();
Console.WriteLine("cat.GetType() -> " + cat.GetType());
Console.WriteLine("dog.GetType() -> " + dog.GetType());
當使用這些對象時:
public interface IInterface<T> { }
public class CatImplementer : IInterface<Cat> { }
public class Fallback<T> : IInterface<T> { }
public class Cat { }
public class Dog { }
會打印:
cat.GetType() -> ConsoleApplication1.CatImplementer
dog.GetType() -> ConsoleApplication1.Fallback`1[ConsoleApplication4.Dog]
如果不爲你在你的實際使用情況(使用城堡3.3)工作,則必須有一個關鍵部分,這是不同的。隨意添加到您的問題,如果是這樣的話。
令人驚歎........ – David 2014-10-23 08:08:06