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