Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast object to generic type?

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

like image 679
theateist Avatar asked Jan 20 '23 10:01

theateist


2 Answers

Your code for Do and GetInstance should be generic too. Basicly it could look something like this

public T Do<T> ()
{
     return ObjectFactory.GetInstance<T>().Create();
}
like image 157
Valentin Kuzub Avatar answered Jan 28 '23 09:01

Valentin Kuzub


Couldn't you make Do() generic?

var m = Do<B>();

public T Do<T>()
{ 
   var builder = (IViewModelBuilder<T>)ObjectFactory.GetInstance(typeof(T));
   return builder.Create("");
}

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:

var m = Do(typeof(B));

public object Do(Type t)
{ 
   dynamic builder = ObjectFactory.GetInstance(t);
   return builder.Create("");
}
like image 38
svick Avatar answered Jan 28 '23 09:01

svick