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);
                }
            }

  • 相关阅读:
    在不申请第三方变量的情况下交换a和b
    指针(pointer)的使用
    数组和链表(arrary & linkedlist)
    Python的学习计划
    Python——函数
    Python——文件操作
    Python——深浅Copy
    我发现的新大陆——好用的~
    我发现的新大陆——好吃儿的
    我发现的新大陆——好玩儿的
  • 原文地址:https://www.cnblogs.com/wahaccp/p/3254853.html
Copyright © 2011-2022 走看看