簡體   English   中英

獲取繼承類的FieldInfo

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM