willy
willy

Reputation: 11

Using reflection to select some properties

how to select only some properties of class. let's say I have class

public class BaseEntity
{
   protected string _createdBy;
   protected DateTime _createdDate;
   protected string _updatedBy;
   protected DateTime _updatedDate;

   //set get
}

public class User : BaseEntity
{
   private string _username;
   private string _password;
   private Employee _employee;

   //set get 
}

I only want to select Username, Password, and Employee, not CreatedBy, CreatedDate, UpdatedBy, and UpdatedDate. Is there any way to do this? I've tried searching by google, but i found nothing so I can only hardcode it, like this

if (!propertyInfo.Name.Equals("CreatedDate") ||
!propertyInfo.Name.Equals("CreatedBy"))
{
}

Upvotes: 0

Views: 208

Answers (1)

drharris
drharris

Reputation: 11214

You should use the BindingFlags.DeclaredOnly flag in your Type.GetProperties() call, which will ignore inherited properties.

Upvotes: 7

Related Questions