简体   繁体   中英

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"))
{
}

您应该在Type.GetProperties()调用中使用BindingFlags.DeclaredOnly标志,该标志将忽略继承的属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM