如何在C#中从基类中按名称获取派生类MEMBERS?

How to get derived class MEMBERS by name, from base class, in C#? [duplicate]
2020-11-21
  •  译文(汉语)
  •  原文(英语)

我有基类CommonClass-抽象父类.从中我有许多派生类,所有派生类都只有一个新的相同类型ObjDescr的不同成员.简短的例子

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为我提供了很好的服务,但是现在我想做一个通用的方法,可以向其发送任何其他派生的子类.知道成员名称,它将迭代一些新的派生成员.

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;
}

我需要直接访问名称为memb_name实例UnknownDerived的成员.

编辑:我不同意这个问题与其他重复.解决方案是相同的,我同意.但是问题就不同了.在我询问之前,我用Google搜索和搜索,但找不到答案.如果其他人希望从名称中获取实例的类成员,则将很方便.

速聊1:
我尝试使用GetMember,GetField,但是它们返回类型的成员,而不是特定实例的成员.
解决过程1

使用Reflection,您可以访问该属性:

public void SuperDuperUniversal(CommonClass unknownDerived, string memb_name)
{
    var member = unknownDerived.GetType().GetProperty(memb_name).GetValue(unknownDerived, null) as ObjDescr;

    string desc = member.Description;
}
速聊1:
似乎GetValue()(您可以在其中放置派生对象的实例)是我在反射中找不到的方法.我将尝试此操作,并接受答案(如果可行).

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.

Talk1:
I tried to use GetMember, GetField, but they return member of type, not the member of this particular instance.
Solutions1

Using Reflection, you could get access to that property:

public void SuperDuperUniversal(CommonClass unknownDerived, string memb_name)
{
    var member = unknownDerived.GetType().GetProperty(memb_name).GetValue(unknownDerived, null) as ObjDescr;

    string desc = member.Description;
}
Talk1:
Seems GetValue(), where you can put the instance of derived object, is the method i missed to find in Reflection. I will try this, and Accept answer, if it works.
转载于:https://stackoverflow.com/questions/28362473/how-to-get-derived-class-members-by-name-from-base-class-in-c

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入