【发布时间】:2013-10-31 01:40:40
【问题描述】:
我有嵌套列表的实体:
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,如何迭代它?
附言
我正在使用 .NET 4.5
【问题讨论】: