zoukankan      html  css  js  c++  java
  • C#深拷贝 反射实现

            /// <summary>
            /// 对象拷贝
            /// </summary>
            /// <param name="obj">被复制对象</param>
            /// <returns>新对象</returns>
            private object CopyOjbect(object obj)
            {
                if (obj == null)
                {
                    return null;
                }
                Object targetDeepCopyObj;
                Type targetType = obj.GetType();
                //值类型  
                if (targetType.IsValueType == true)
                {
                    targetDeepCopyObj = obj;
                }
                //引用类型   
                else
                {
                    targetDeepCopyObj = System.Activator.CreateInstance(targetType);   //创建引用对象   
                    System.Reflection.MemberInfo[] memberCollection = obj.GetType().GetMembers();
    
                    foreach (System.Reflection.MemberInfo member in memberCollection)
                    {
                        //拷贝字段
                        if (member.MemberType == System.Reflection.MemberTypes.Field)
                        {
                            System.Reflection.FieldInfo field = (System.Reflection.FieldInfo)member;
                            Object fieldValue = field.GetValue(obj);
                            if (fieldValue is ICloneable)
                            {
                                field.SetValue(targetDeepCopyObj, (fieldValue as ICloneable).Clone());
                            }
                            else
                            {
                                field.SetValue(targetDeepCopyObj, CopyOjbect(fieldValue));
                            }
    
                        }//拷贝属性
                        else if (member.MemberType == System.Reflection.MemberTypes.Property)
                        {
                            System.Reflection.PropertyInfo myProperty = (System.Reflection.PropertyInfo)member;
    
                            MethodInfo info = myProperty.GetSetMethod(false);
                            if (info != null)
                            {
                                try
                                {
                                    object propertyValue = myProperty.GetValue(obj, null);
                                    if (propertyValue is ICloneable)
                                    {
                                        myProperty.SetValue(targetDeepCopyObj, (propertyValue as ICloneable).Clone(), null);
                                    }
                                    else
                                    {
                                        myProperty.SetValue(targetDeepCopyObj, CopyOjbect(propertyValue), null);
                                    }
                                }
                                catch (System.Exception ex)
                                {
    
                                }
                            }
                        }
                    }
                }
                return targetDeepCopyObj;
            }
  • 相关阅读:
    a.default.ERROR.httpAjax is not a function
    Java heap space
    jmeter
    sql注入
    数据库查前三名
    maven
    国际化变现应用分析
    百度应用部署秘籍
    如何建立起一套有效的APP监控体系
    第三方舆情收集与质量闭环建设
  • 原文地址:https://www.cnblogs.com/xiashenbin/p/5648846.html
Copyright © 2011-2022 走看看