[英]Get FieldInfo of inherited class
在C#中,我具有以下列方式派生的类:
MyClass1 <- MyClass2 <- MyClass3 <- MyClass4 (The root class is MyClass1)
现在,我有MyClass4 myClass4的实例。 如何获取在MyClass2中声明的私有字段信息? 我可以执行以下操作:
FieldInfo[] fields = model.GetType().BaseType.BaseType.
GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (FieldInfo fld in field)
{
....
}
如果继承级别未知,该怎么办?
您知道要在MyClass2
中查找字段吗? 如果是这样,请继续读取CurrentType.BaseType
直到CurrentType == typeof(MyClass2)
为止。
ow
Type lCurrentType = model.GetType();
while (lCurrentType != typeof(MyClass2) && lCurrentType != null)
{
lCurrentType = lCurrentType.BaseType;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.