有什么样的Python在C#GETATTR()? [英] Is there something like Python's getattr() in C#?
本文介绍了有什么样的Python在C#GETATTR()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么样的 Python的GETATTR()在C#中?我想通过读取其中包含控件的名称将在窗口列表来创建一个窗口。
Is there something like Python's getattr() in C#? I would like to create a window by reading a list which contains the names of controls to put on the window.
推荐答案
有还 Type.InvokeMember 。
public static class ReflectionExt
{
public static object GetAttr(this object obj, string name)
{
Type type = obj.GetType();
BindingFlags flags = BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.GetProperty;
return type.InvokeMember(name, flags, Type.DefaultBinder, obj, null);
}
}
哪位能像使用:
Which could be used like:
object value = ReflectionExt.GetAttr(obj, "PropertyName");
或(如扩展方法):
object value = obj.GetAttr("PropertyName");
这篇关于有什么样的Python在C#GETATTR()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文