C# 反射獲取屬性GetProperty無效的問題解決方法

C#當中獲取屬性有種情況爲,該屬性沒有get和set函數,則該屬性非屬性,實際爲字段。因此需要使用以下方法來獲取:

Type type = typeof(YourClass);
string propertyName = "yourField";
const BindingFlags InstanceBindFlags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
while (type != null)
{
    property = type.GetField(propertyName, InstanceBindFlags);
    if (property != null)
    {
        break;
    }
}
int value = (int)property.GetValue(BuildingWindow.instance);

以上

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章