开发者

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;
   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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜