我有一个带有嵌套列表的实体:
public class Article : MyEntityBase
{
public Article()
{
Tags = new List<Tag>();
}
[MyAttribute]
public string Title { get; set; }
[MyAttribute]
public virtual List<Tag> Tags { get; set; }
}
public class Tag : EntityBase
{
public string Title { get; set; }
}
public abstract class MyEntityBase
{
public Guid Id { get; set; }
}
我还有一个函数,可以收集所有用[MyAttribute]
标注的属性,并对它们执行某些操作:
public function OperateWithAttributes(IEnumerable<PropertyInfo> properties)
{
foreach (var p in properties)
{
if (p.PropertyType == typeof(string))
{
// do something
}
else if (/* there are code that check property type is List<T> */)
{
/* there are code that iterate list */
}
}
}
问题:
- 如何将属性类型与
List<T>
进行比较? - 如果我知道列表是从
EntityBase
继承而来,如何迭代它?
P.S
我正在使用.NET 4.5