vendredi 6 février 2015

How to get derived class MEMBERS by name, from base class, in C#? [duplicate]


This question already has an answer here:




I have base class CommonClass - abstract parent. From it I have many derived classes, all have only new different members of same type ObjDescr. A shorten example



public class CommonClass
{
// many common methods, handlers and properties
}

// many derived classes like this one
public class Orders : CommonClass
{
public ObjDescr Date { get; private set }
public ObjDescr Place { get; private set }
}


CommonClass service me well, but now i wanna to do an universal method, to which i can send any different derived child class. It will iterate some of the new derived members, knowing member names.



public SuperDuperUniversal( CommonClass UnknownDerived, string memb_name )
{
// I do access to members now only WHEN i know the class
Orders Order1 = (Orders)UnknownDerived;
ObjDescr aPlace1 = Orders.Place;

// I wanna to have access like
ObjDescr aPlace2 = UnknownDerived.Get_ObjDescr_ByName(memb_name);
string aPlaceName = aPlace2.Description;
}


I need to have access directly to member with name memb_name instance UnknownDerived.


Edit: I disagree that this question is duplicate with other. Solution is same, i agree. But question is different. I googled and search, before i ask it, and fail to find an answer. It will be handy, if someone else is looking to get class members of instance, from name, to find this answer.






Aucun commentaire:

Enregistrer un commentaire