我有一个Person继承的类EntityBase:
public class Person : EntityBase
{
virtual public string FirstName { get; set; }
virtual public string LastName { get; set; }
virtual public IList<Asset> Assets { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和
public class EntityBase : IEntity
{
public virtual long Id { get; protected set; }
public virtual string Error { get; protected set; }
}
Run Code Online (Sandbox Code Playgroud)
我需要获得自习Person类的属性列表:
var entity = preUpdateEvent.Entity;
foreach (var item in entity.GetType().GetProperties()) //only FirstName & LastName
{
if (item.PropertyType == typeof(String))
item.SetValue(entity, "XXXXX" ,null);
}
Run Code Online (Sandbox Code Playgroud)
现在GetProperties()包括:FirstName, LastName, Id, Error但我只需要拥有自己的Person属性:FirstName, LastName
如何获取仅定义的属性Person?
使用
var properties = typeof(Person).GetProperties(BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.DeclaredOnly);
Run Code Online (Sandbox Code Playgroud)
该DeclaredOnly值记录如下:
指定仅应考虑在提供的类型的层次结构级别声明的成员.不考虑继承的成员.
| 归档时间: |
|
| 查看次数: |
2112 次 |
| 最近记录: |