簡體   English   中英

Type.GetProperties方法

[英]Type.GetProperties Method

我有一個這樣的課:

class ItemList
{
    Int64 Count { get; set; }
}

當我寫這個:

ItemList list = new ItemList ( );

Type type = list.GetType ( );
PropertyInfo [ ] props = type.GetProperties ( );

我得到一個空數組的道具。

為什么? 是因為GetProperties不包含自動屬性嗎?

問題是GetProperties默認只返回Public屬性。 在C#中,默認情況下成員不公開(我相信它們是內部的)。 試試這個

var props = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic);

BindingFlags枚舉相當靈活。 上面的組合將返回該類型上的所有非公共實例屬性。 您可能想要的是所有實例屬性,無論可訪問性如何。 在這種情況下,請嘗試以下方法

var flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var props = type.GetProperties(flags);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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