zoukankan      html  css  js  c++  java
  • Linq-Detach方法--删除对象的时候如有关系导制错误去关系

     public static void Detatch(Object entity)
            {
                Type t = entity.GetType();
                System.Reflection.PropertyInfo[] properties = t.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
                foreach (var property in properties)
                {
                    string name = property.Name;
                    if (property.PropertyType.IsGenericType &&
                    property.PropertyType.GetGenericTypeDefinition() == typeof(EntitySet<>))
                    {
                        property.SetValue(entity, null, null);
                    }
                }
                System.Reflection.FieldInfo[] fields = t.GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                foreach (var field in fields)
                {
                    string name = field.Name;
                    if (field.FieldType.IsGenericType &&
                    field.FieldType.GetGenericTypeDefinition() == typeof(EntityRef<>))
                    {
                        field.SetValue(entity, null);
                    }
                }
                System.Reflection.EventInfo eventPropertyChanged = t.GetEvent("PropertyChanged");
                System.Reflection.EventInfo eventPropertyChanging = t.GetEvent("PropertyChanging");
                if (eventPropertyChanged != null)
                {
                    eventPropertyChanged.RemoveEventHandler(entity, null);
                }
                if (eventPropertyChanging != null)
                {
                    eventPropertyChanging.RemoveEventHandler(entity, null);
                }
            }

  • 相关阅读:
    Moo.fx 超级轻量级的 javascript 特效库
    Oracle 异常错误处理
    变被动为主动
    数据结构定义
    Redis 一个keyvalue存储系统 简介
    使用Container.ItemIndex获取Repeater、Gridview行的序号的简单方法
    ORACLE SQL:经典查询练手第一篇
    MongoDB 一个基于分布式文件存储的数据库
    dojo Quick Start/dojo入门手册面向对象,定义Class
    面向过程分析与面向对象分析之间的区别
  • 原文地址:https://www.cnblogs.com/wahaccp/p/3254853.html
Copyright © 2011-2022 走看看