使用反射选择一些属性

0 c# reflection

如何只选择类的一些属性.让我说我上课了

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 
}
Run Code Online (Sandbox Code Playgroud)

我只想选择Username,Password和Employee,而不是CreatedBy,CreatedDate,UpdatedBy和UpdatedDate.有没有办法做到这一点?我试过谷歌搜索,但我什么都没找到,所以我只能硬编码,就像这样

if (!propertyInfo.Name.Equals("CreatedDate") ||
!propertyInfo.Name.Equals("CreatedBy"))
{
}
Run Code Online (Sandbox Code Playgroud)

drh*_*ris 7

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