lundi 9 juillet 2018

How to get specific type instance (not object type) by reflection in c#

guys. I tried to know the way to get specific type instance by reflection. there ware many way to get object type instance by reflection. but, my conclusion is there is no way to get specific type instance by reflection.

I tried to do it like this

I made function that returns specific typed instance. and I called it by reflection. but It still returns object type instance. the function I made is like this. so simple.

public class MTT
{
    public MTT GetModel()
    {
        return new MTT();
    }
}

So, I didn't want to, but I made the function like this.

 if (headerOrder == 1)
     parsedModel.MSH = (MSH)header;
 else if (headerOrder == 2)
     parsedModel.FAC = (FAC)header;
 else if (headerOrder == 3)
     parsedModel.PRD = (PRD)header;
 else if (headerOrder == 4)
     parsedModel.PID = (PID)header;

Please let me know if there is the way to get specific type instance by reflection.

Thank you.





Aucun commentaire:

Enregistrer un commentaire