【发布时间】:2010-09-20 12:07:24
【问题描述】:
我正在尝试在 .NET 4 中为我的 POCO 对象创建一个基类,它将有一个 Include(string path) 方法,其中 path 是一个“。”要枚举的继承类的嵌套 ICollection 属性的分隔导航路径。
例如,给定以下类;
public class Region
{
public string Name { get; set; }
public ICollection<Country> Countries { get; set; }
}
public partial class Region : EntityBase<Region> {}
public class Country
{
public string Name { get; set; }
public ICollection<City> Cities { get; set; }
}
public partial class Country : EntityBase<Country> {}
public class City
{
public string Name { get; set; }
}
public partial class City : EntityBase<City> {}
我希望能够做这样的事情;
Region region = DAL.GetRegion(4);
region.Include("Countries.Cities");
到目前为止,我有以下内容;
public class EntityBase<T> where T : class
{
public void Include(string path)
{
// various validation has been omitted for brevity
string[] paths = path.Split('.');
int pathLength = paths.Length;
PropertyInfo propertyInfo = type(T).GetProperty(paths[0]);
object propertyValue = propertyInfo.GetValue(this, null);
if (propertyValue != null)
{
Type interfaceType = propertyInfo.PropertyType;
Type entityType = interfaceType.GetGenericArguments()[0];
// I want to do something like....
var propertyCollection = (ICollection<entityType>)propertyValue;
foreach(object item in propertyCollection)
{
if (pathLength > 1)
{
// call Include method of item for nested path
}
}
}
}
}
显然,“var list = ...>”行不起作用,但您希望能明白要点,并且除非 propertyCollection 是可枚举的,否则 foreach 将不起作用。
所以这是最后一点,即当我直到运行时才知道 T 的类型时,如何枚举一个类的 ICollection 属性?
谢谢
【问题讨论】:
标签: c# reflection