程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#對象深拷貝方法

C#對象深拷貝方法

編輯:C#入門知識

對象的拷貝是編程中常用到的,下面提供一個對象深拷貝的方法,挺不錯的

[csharp] 
public object Copy(this object obj) 
        { 
            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, Copy(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) 
                        { 
                            object propertyValue = myProperty.GetValue(obj, null); 
                            if (propertyValue is ICloneable) 
                            { 
                                myProperty.SetValue(targetDeepCopyObj, (propertyValue as ICloneable).Clone(), null); 
                            } 
                            else 
                            { 
                                myProperty.SetValue(targetDeepCopyObj, Copy(propertyValue), null); 
                            } 
                        } 
 
                    } 
                } 
            } 
            return targetDeepCopyObj; 
        } 
public object Copy(this object obj)
        {
            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, Copy(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)
                        {
                            object propertyValue = myProperty.GetValue(obj, null);
                            if (propertyValue is ICloneable)
                            {
                                myProperty.SetValue(targetDeepCopyObj, (propertyValue as ICloneable).Clone(), null);
                            }
                            else
                            {
                                myProperty.SetValue(targetDeepCopyObj, Copy(propertyValue), null);
                            }
                        }

                    }
                }
            }
            return targetDeepCopyObj;
        }
 

 作者:cangkukuaimanle
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved