GenericClass
ジェネリック型 ( ) に依存する依存関係を持つジェネリック クラス ( ) がありIGenericDependency
ます。この依存関係も一般的です。
public class GenericClass<T>
{
private readonly IGenericDependency;
}
型パラメータは実行時までわかりません。
これまでのところ、私はこれを行ってきました:
Funcを注入しています。
public class GenericClass<T> : IGenericClass<T> where T:class , new()
{
private readonly IGenericDependency _genericDependency;
public GenericClass(Func<TypeIGenericDependency>> factory)
{
_genericDependency = factory(T);
}
}
そして再登録コード:
builder.RegisterGeneric(typeof (GenericClass<>)).As(typeof (IGenericClass<>));
builder.Register<Func<Type, IGetDataCollection>>(c =>
{
var context = c.Resolve<IComponentContext>();
return type =>
{
if(type.Name.EndsWith("Entity"))
{
return (IGenericDependency)
context.Resolve(typeof (GetEntityCollection<>)
.MakeGenericType(type));
}
if(type.Name.EndsWith("TypedList"))
{
return (IGenericDependency)
context.Resolve(typeof (GetTypedList<>)
.MakeGenericType(type));
}
throw new NotSupportedException("Not supported type");
};
});
これを行う別の方法があるかどうか疑問に思っています。