Castle Windsor
はそのようなことをサポートしていませんが、単純なヘルパー メソッドで実現できます。
public static void RegisterAccordingToISuperType<T>(this IWindsorContainer container)
{
if (typeof (T).GetInterfaces().Contains(typeof (ISuperType)))
container.Register(Component.For<IRepository<T>>()
.ImplementedBy<SuperRepository<T>>());
else
container.Register(Component.For<IRepository<T>>()
.ImplementedBy<Repository<T>>());
}
次に、登録:
container.RegisterAccordingToISuperType<SuperType>();
container.RegisterAccordingToISuperType<int>();
そして、解決は次のようになります。
var super = container.Resolve<IRepository<SuperType>>();
var intRepo = container.Resolve<IRepository<int>>();
別のオプションは、 の追加パラメータですComponent.For
。次に、 Type から継承するすべての型 (たとえば) を取得して登録します。
private static void Register(...)
{
foreach (var type in GetInheriteTypes())
{
container.Register(Component.For(typeof(IRepository<>),type)
.ImplementedBy(typeof(SuperRepository<>)));
}
container.Register(Component.For(typeof(IRepository<>))
.ImplementedBy(typeof(Repository<>)));
}
private static IEnumerable<Type> GetInheriteTypes()
{
var listOfBs = (from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
from assemblyType in domainAssembly.GetTypes()
where assemblyType.GetInterfaces().Contains(typeof(ISuperType))
select assemblyType).ToArray();
return listOfBs;
}