[英]Get FieldInfo of inherited class
In C# I have classes which are derived in the following way: 在C#中,我具有以下列方式派生的类:
MyClass1 <- MyClass2 <- MyClass3 <- MyClass4 (The root class is MyClass1)
Now I have an instance of MyClass4 myClass4. 现在,我有MyClass4 myClass4的实例。 How to get a private field info declared in MyClass2?
如何获取在MyClass2中声明的私有字段信息? I can do the following:
我可以执行以下操作:
FieldInfo[] fields = model.GetType().BaseType.BaseType.
GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (FieldInfo fld in field)
{
....
}
What if the inheritance level is unknown? 如果继承级别未知,该怎么办?
Do you know you are looking for a field in MyClass2
? 您知道要在
MyClass2
中查找字段吗? If so, keep reading CurrentType.BaseType
until CurrentType == typeof(MyClass2)
. 如果是这样,请继续读取
CurrentType.BaseType
直到CurrentType == typeof(MyClass2)
为止。
Iow ow
Type lCurrentType = model.GetType();
while (lCurrentType != typeof(MyClass2) && lCurrentType != null)
{
lCurrentType = lCurrentType.BaseType;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.