![](/img/trans.png)
[英]Is there a way to get the child class from the parent and check (and use) methods and properties?
[英]How to get properties from parent class
我想使用GetProperties
通過孩子從父類中獲取屬性,盡管進行了研究,但還是沒有成功。
我嘗試了下一個沒有任何結果:
PropertyInfo[] fields = t.GetProperties();
PropertyInfo[] fields1 = t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
PropertyInfo[] propNames = t.BaseType.GetProperties( BindingFlags.Public | BindingFlags.Instance);
只是從子類獲得屬性,但不從父類獲得屬性。
班級
public class A: B
{
public string a1 { get; set; }
public string a2 { get; set; }
public string a3 { get; set; }
public string a4 { get; set; }
}
public class B
{
public string b1;
}
使用此代碼,我得到A
的屬性,而不是B
的屬性。
此代碼有效嗎? 我是否需要在某個地方進行配置?
在您的聲明中
public class B
{
public string b1;
}
b1
是一個字段 ,而不是一個屬性。 你應該
使用GetFields()
:
FieldInfo[] fields = t.GetFields();
這將獲得字段(按預期方式)-請注意, 文檔中指出
通常,應僅將字段用於具有私有或受保護的可訪問性的變量。
將b1
設為屬性,例如,添加{ get; set; }
{ get; set; }
{ get; set; }
訪問者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.