我有一个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; }
}
和
public class EntityBase : IEntity
{
public virtual long Id { get; protected set; }
public virtual IEnumurable<string> Errors { get; protected set; }
}
我需要获取Person
不是集合的类的属性列表
现在GetProperties()
包括:FirstName, LastName, Assets, Id, Errors
但我不需要数组属性:FirstName, LastName, Id
如何获取未收集的属性?
您可以按属性的返回类型进行过滤。我怀疑您想过滤掉实现的所有东西IEnumerable
,但不是 string
(实现的IEnumerable<char>
,但是您想要保留的)。所以像这样:
var properties = type.GetProperties()
.Where(p => p.PropertyType == typeof(string) ||
!typeof(IEnumerable).IsAssignableFrom(p.PropertyType));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句