【发布时间】:2016-02-21 18:47:24
【问题描述】:
我正在为断开连接的情况下的实体框架更新构建自己的通用解决方案。可以采用许多不同的方法,但我决定使用自定义属性装饰我的实体内部的 ICollection 属性,以便我可以检查这些集合中每个实体的状态。这是一个带有导航属性的示例实体:
public class SomeEntity
{
public int TaskId{ get; set; }
public string Instruction { get; set; }
[EntityNavigationProperty]
public virtual ICollection<TaskExecutor> Executors { get; set; }
}
public class TaskExecutor
{
public int TaskExecutorId { get; set; }
public int TaskId { get; set; }
public virtual Task Task { get; set; }
}
public class EntityNavigationProperty : Attribute {}
我有一个通用的 Update 方法,我打算用它来更新任何类型的实体,这将确保相关实体也得到正确更新。
public void Update(TEntity entity)
{
PropertyInfo[] properties = entity.GetType().GetProperties();
foreach (PropertyInfo pi in properties)
{
if (Attribute.IsDefined(pi, typeof(EntityNavigationProperty)))
{
foreach (//here I need to iterate through the ICollection object)
{
}
}
}
}
现在,假设我正在向上述更新方法发送一个 Task 实例。在第 3 行中,当迭代器到达 Executors 属性时,第 5 行中的条件解析为 true。现在我需要遍历 Executors 属性并执行适当的任务。对于这种特殊情况,在第 6 行我可以说:
foreach (var item in (pi.GetValue(entity) as ICollection<TaskExecutor>))
但是我如何确定在 ICollection<T> 中输入什么而不是 T 呢?
【问题讨论】:
-
我已经重新格式化了你的一些代码,并为你的类命名。检查它们是否正常
标签: reflection generic-collections generic-type-argument