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);

以上

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