Logo Questions Linux Laravel Mysql Ubuntu Git Menu

How get properties for a inherits class




I have a Person class that inherits EntityBase:

public class Person : EntityBase
   virtual public string FirstName { get; set; }
   virtual public string LastName { get; set; } 
   virtual public IList<Asset> Assets { get; set; }   



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 self Person class:

var entity = preUpdateEvent.Entity;

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

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

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

like image 525
Ehsan Avatar asked Feb 22 '23 00:02


1 Answers


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

The DeclaredOnly value is documented like this:

Specifies that only members declared at the level of the supplied type's hierarchy should be considered. Inherited members are not considered.

like image 71
Jon Skeet Avatar answered Mar 08 '23 10:03

Jon Skeet