How do you get the all properties of a class and its base classes (up the hierarchy) with Reflection? (C#)

前端 未结 5 1006
夕颜
夕颜 2020-11-30 08:48

So what I have right now is something like this:

PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public);

where obj

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 09:26

    Use this:

    PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
    

    EDIT: Of course the correct answer is that of Jay. GetProperties() without parameters is equivalent to GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static ). The BindingFlags.FlattenHierarchy plays no role here.

提交回复
热议问题