Search code examples
c#.netreflectionbindingflags

How get properties for a inherits class by this condition


I have a Person class that inherits PersonBase and this second class inherits EntityBase:

public class Person : PersonBase
{        
   virtual public string FirstName { get; set; }
   virtual public string LastName { get; set; } 
}

And

public class PersonBase : EntityBase
{        
   virtual public string GroupName { get; set; }
}

And

public class EntityBase : IEntity
{    
   public virtual long Id { get; protected set; }
   public virtual string Error { get; protected set; }
}

I need to get list of properties of Person and PersonBase classes :

var entity = preUpdateEvent.Entity;

foreach (var item in entity.GetType().GetProperties()) //only FirstName & LastName & GroupName
{
   if (item.PropertyType == typeof(String))               
      item.SetValue(entity, "XXXXX" ,null);
} 

Now GetProperties() is include : FirstName, LastName, GroupName, Id, Error but I need only own Person properties namely : FirstName, LastName, GroupName

Of Course I has been used below code but it is not suitable for me because it is only include properties of Person class.

var properties = typeof(Person).GetProperties(BindingFlags.Public |
                                                  BindingFlags.Instance |
                                                  BindingFlags.DeclaredOnly);

How can I get the properties that are only defined on Person and PersonBase classes?


Solution

  • Here's a generic solution to your problem, in the form of an extension method:

    public static PropertyInfo[] GetPropertiesUpTo<T>(this Type type, BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)
    {
        return type.GetProperties(flags)
                   .Where(p => p.DeclaringType == typeof(T) || p.DeclaringType.IsSubclassOf(typeof(T)))
                   .ToArray();
    }
    

    You can use it like this:

    var properties = typeof(Person).GetPropertiesUpTo<PersonBase>();