Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置实体集<;t>;使用反射将属性设置为默认值_C#_Linq_Reflection_Generics - Fatal编程技术网

C# 设置实体集<;t>;使用反射将属性设置为默认值

C# 设置实体集<;t>;使用反射将属性设置为默认值,c#,linq,reflection,generics,C#,Linq,Reflection,Generics,我正在尝试编写用于分离linq类的通用代码。我目前拥有的是: public void Detach() { this.PropertyChanged = null; this.PropertyChanging = null; this.Categories = default(EntitySet<Categories>); this.Jobs = default(EntitySet<Jobs>); this.Tasks= defa

我正在尝试编写用于分离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;
this.Categories=默认值(EntitySet);
this.Jobs=默认值(EntitySet);
this.Tasks=默认值(EntitySet);
}
这一切都很好,但是我的数据库中有几百个表,专门为每一个表执行此操作将是一项耗时的任务。我要寻找的是一些通用的东西,我几乎可以用于每个类定义,类似于:

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
    }
}
public void Detach()
{
this.PropertyChanged=null;
this.PropertyChanging=null;
foreach(System.Reflection.PropertyInfo\u prop在此.GetType().GetProperties()中)
{
//如果_prop的类型为EntitySet,则将其设置为默认(EntitySet);
//TODO:在此处完成代码
}
}
我不确定如何编写代码来执行注释中描述的任务。这是可以做到的,还是我正在尝试做一些在当前框架下做不到的事情


编辑:将EntityRef更改为EntitySet。

最简单的方法是通过反射调用.dbml生成的initialize方法:

public void Detach()
{
  GetType().GetMethod("Initialize", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(this, null);
} 
为了生成Initialize方法,必须将dbml文件中的Serialization属性设置为“单向”(右键单击并选择properties,您将在property inspector中看到它)

是的,我感觉到了你的痛苦