I have a generic class (GenericClass
) which is having a dependency depending on the generic type (IGenericDependency
). This dependency is also generic.
public class GenericClass<T>
{
private readonly IGenericDependency;
}
The type parameter is not known until runtime.
So far I've done this:
I'm injecting a Func.
public class GenericClass<T> : IGenericClass<T> where T:class , new()
{
private readonly IGenericDependency _genericDependency;
public GenericClass(Func<TypeIGenericDependency>> factory)
{
_genericDependency = factory(T);
}
}
And the reistration code:
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");
};
});
I'm wondering if there is another way to do this.