繁体   English   中英

按字符串引用字段

[英]Referencing a field by string

一个非常简单的问题,但我无法通过25分钟的谷歌找到一个很好的答案〜

我想引用存储在名为(string)〜的字段中的对象

像〜的东西

private string ButtonName;
public ActionPanel ActionPanel;
private object reference;

void main(){
     ActionPanel = new ActionPanel();
     reference = ActionPanel.ChangeSelectedActionBundle.(ButtonName);

    }

我假设我将需要使用反射,但我不太确定这样做的正确方法:(

反射将像这样工作(假设您试图获取具有ButtonName值的名称“ChangeSelectedActionBundle”的属性):

Type type = ActionPanel.ChangeSelectedActionBundle.GetType();
PropertyInfo property = type.GetProperty(ButtonName);
object value = property.GetValue(ActionPanel.ChangeSelectedActionBundle, null);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM