lundi 8 août 2016

Dynamically convert object using reflection

I have a list of objects of different types, which I need to do different stuff with depending on their type. Specifically, two of the types share a method which I want to use for either of them. What I'm currently trying is this:

if (foo is Type01 || foo is Type02) {
    Type tempType = foo.GetType();
    outList.Add((foo as tempType).ToOutType());
    return outList;
}

This, however, doesn't work, as the compiler apparently cannot use the Type dynamically. Is there any way of achieving this or will I have to put the two options in different if clauses?





Aucun commentaire:

Enregistrer un commentaire