I have many classes that implements IBuilder<> interface such the ones below
UPDATED:
each Model1, Model2… inherits from IModel
public class A : IBuilder<Model1>
{
public Model1 Create(string param)
{
return new Model1();
}
}
public class B : IBuilder<Model2>
{
public Model2 Create(string param)
{
return new Model2();
}
}
I’m using StructureMap to register all classes that inherit IBuilder<>
Scan(x =>
{
x.TheCallingAssembly();
x.AddAllTypesOf(typeof(IViewModelBuilder<>));
});
UPDATED
Now, every time I need to get model of some Module I call Do function
public IModel Do(Module module)
{
//ModelSettings is taken from web.config
var builderType = Type.GetType(string.Format("{0}.{1}ModelBuilder,{2}", ModelSettings.Namespace, module.CodeName, ModelSettings.Assembly));
var builder = ObjectFactory.GetInstance(t) as IViewModelBuilder<>;
return builder.Create("");
}
I get compilation error in the line ObjectFactory.GetInstance(t) as IViewModelBuilder<>.
Many posts suggest to create NOT generic interface(IViewModelBuilder) and let the generic one to inherit it. And then I could make the casting like
ObjectFactory.GetInstance(t) as IViewModelBuilder
Is this the only way?
Thank you
Couldn’t you make
Do()generic?If you can’t, using non-generic interface is probably your best bet, but there are other options like using reflection or C# 4’s
dynamic: