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

  • 相关阅读:
    Haskell 差点儿无痛苦上手指南
    HighCharts 具体使用及API文档说明
    又一道软通动力7K月薪面试题——银行业务调度系统
    [AngularJS + Webpack] require directives
    [AngularJS + Webpack] Using Webpack for angularjs
    [Whole Web] [AngularJS] Localize your AngularJS Application with angular-localization
    [React] React Fundamentals: Mixins
    [React] React Fundamentals: Component Lifecycle
    [React ] React Fundamentals: Component Lifecycle
    [React] React Fundamentals: Component Lifecycle
  • 原文地址:https://www.cnblogs.com/wahaccp/p/3254853.html
Copyright © 2011-2022 走看看