开发者

Using reflection to select some properties

开发者 https://www.devze.com 2023-02-07 06:12 出处:网络
how to select only some properties of class. let\'s say I have class public class BaseEntity { protected string _createdBy;

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;
   pr开发者_JAVA百科otected 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"))
{
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消