Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

reflection

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 declared in the derived class.

public abstract class BaseMsClass
{
    public string CommonParam { get; set; }
}

public class MsClass : BaseMsClass
{
    public string Id { get; set; }
    public string Name { get; set; }

    public MsClass()
    { }
}

var msClass = new MsClass
{
    Id = "1122",
    Name = "Some name",
    CommonParam = "param of the base class"
};

So, I would like to quickly find out that CommonParam is an inherited parameter and Id, Name are params declared in MsClass. Any suggestions?

Attempt to use declared only flag returns me empty PropertyInfo array

Type type = msClass.GetType();
type.GetProperties(System.Reflection.BindingFlags.DeclaredOnly)

-->{System.Reflection.PropertyInfo[0]}

However, GetProperties() returns all properties of inheritance hierarchy.

type.GetProperties()

-->{System.Reflection.PropertyInfo[3]}
-->[0]: {System.String Id}
-->[1]: {System.String Name}
-->[2]: {System.String CommonParam}

Did I miss something?

like image 490
Maxim Avatar asked Apr 01 '13 16:04

Maxim


People also ask

What is inherited by a derived class from base class?

The derived class inherits all members and member functions of a base class. The derived class can have more functionality with respect to the Base class and can easily access the Base class. A Derived class is also called a child class or subclass.

Does a derived class inherit or doesn't inherit?

The derived class doesn't "inherit" the private members of the base class in any way - it can't access them, so it doesn't "inherit" them. An instance of the derived class contains instances of the private members of the base class, for obvious reasons.

How do you access members of the base class from within a derived class?

You can use both a structure and a class as base classes in the base list of a derived class declaration: If the derived class is declared with the keyword class , the default access specifier in its base list specifiers is private .

What does derived class does not inherit from the base class?

Following are the properties which a derived class doesn't inherit from its parent class : 1) The base class's constructors and destructor. 2) The base class's friend functions. 3) Overloaded operators of the base class.


3 Answers

You can specify Type.GetProperties(BindingFlags.DeclaredOnly) to get the properties that are defined in the derived class. If you then call GetProperties on the base class, you can get the properties defined in the base class.


In order to fetch the public properties from your class, you could do:

var classType = typeof(MsClass);
var classProps = classType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
var inheritedProps = classType.BaseType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
like image 178
Reed Copsey Avatar answered Oct 14 '22 03:10

Reed Copsey


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));
like image 35
cuongle Avatar answered Oct 14 '22 03:10

cuongle


This may helps:

Type type = typeof(MsClass);

Type baseType = type.BaseType;

var baseProperties = 
     type.GetProperties()
          .Where(input => baseType.GetProperties()
                                   .Any(i => i.Name == input.Name)).ToList();
like image 28
Hossein Narimani Rad Avatar answered Oct 14 '22 03:10

Hossein Narimani Rad