How to find out if property is inherited from a base class or declared in derived?

后端 未结 3 857
野的像风
野的像风 2021-02-05 07:28

I have a class that is derived from an abstract class. Getting a type of a derived class I want to find out which properties are inherited from abstract class and which were dec

3条回答
  •  野的像风
    2021-02-05 08:29

    You can check based on the DeclaringType as below:

    var pros = typeof(MsClass).GetProperties()
                              .Where(p => p.DeclaringType == typeof(MsClass));
    

    To get properties from base class you can call similarly:

    var pros = typeof(MsClass).GetProperties()
                              .Where(p => p.DeclaringType == typeof(BaseMsClass));
    

提交回复
热议问题