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.
精彩评论