How to get a Static property with Reflection

后端 未结 8 1296
旧巷少年郎
旧巷少年郎 2020-11-28 05:44

So this seems pretty basic but I can\'t get it to work. I have an Object, and I am using reflection to get to it\'s public properties. One of these properties is static an

8条回答
  •  醉梦人生
    2020-11-28 06:35

    Ok so the key for me was to use the .FlattenHierarchy BindingFlag. I don't really know why I just added it on a hunch and it started working. So the final solution that allows me to get Public Instance or Static Properties is:

    obj.GetType.GetProperty(propName, Reflection.BindingFlags.Public _
      Or Reflection.BindingFlags.Static Or Reflection.BindingFlags.Instance Or _
      Reflection.BindingFlags.FlattenHierarchy)
    

提交回复
热议问题