【发布时间】:2010-10-17 05:30:45
【问题描述】:
我正在尝试编写用于分离 linq 类的通用代码。我目前拥有的是:
public void Detach()
{
this.PropertyChanged = null;
this.PropertyChanging = null;
this.Categories = default(EntitySet<Categories>);
this.Jobs = default(EntitySet<Jobs>);
this.Tasks= default(EntitySet<Tasks>);
}
这一切都很好,但是我的数据库中有数百个表,专门为每一个表执行此操作将是一项耗时的任务。我正在寻找的是通用的东西,我几乎可以将其用于每个类定义,类似于:
public void Detach()
{
this.PropertyChanged = null;
this.PropertyChanging = null;
foreach (System.Reflection.PropertyInfo _prop in this.GetType().GetProperties())
{
// if _prop is of type EntitySet<T> then set it to default(EntitySet<T>);
// TODO: Complete the code here
}
}
我不确定如何编写代码来执行评论所描述的任务。这可以做到吗,还是我正在尝试做一些在当前框架中无法做到的事情?
编辑:将 EntityRef 更改为 EntitySet。
【问题讨论】:
标签: c# linq reflection generics