首页 > 代码库 > 数据结构通用算法(深拷贝+比较)

数据结构通用算法(深拷贝+比较)

 

  1     public class CompareIgnoreAttribute : Attribute  2     {  3     };  4   5     [Serializable] // 深拷贝需要  6     public abstract class DataInfo  7     {  8         // 快速深拷贝  9         public static object copy(object th) 10         { 11             MemoryStream ms = new MemoryStream(); 12             BinaryFormatter bf = new BinaryFormatter(); 13             bf.Serialize(ms, th); 14             ms.Seek(0, 0); 15             object th2 = bf.Deserialize(ms); 16             ms.Close(); 17             return th2; 18         } 19  20         // 比较 21         public static bool Equ(object th1, object th2) 22         { 23             try 24             { 25                 if (th1 == null || th2 == null) 26                 { 27                     if (th1 == null && th2 == null) 28                     { 29                         return true; 30                     } 31                     else 32                     { 33                         return false; 34                     } 35                 } 36  37                 if (th1.GetType() != th2.GetType()) 38                 { 39                     return false; 40                 } 41  42                 if (th1 is IList) 43                 { 44                     IList l1 = th1 as IList; 45                     IList l2 = th2 as IList; 46                     if (l1.Count != l2.Count) 47                     { 48                         return false; 49                     } 50                     else 51                     { 52                         for (int i = 0; i < l1.Count; ++i) 53                         { 54                             if (!DataInfo.Equ(l1[i], l2[i])) 55                             { 56                                 return false; 57                             } 58                         } 59                     } 60                 } 61                 else if (th1.GetType().IsValueType || th1.GetType().IsEnum || th1.GetType() == typeof(string)) 62                 { 63                     if (th1.ToString() != th2.ToString()) 64                     { 65                         return false; 66                     } 67                 } 68                 else 69                 { 70                     PropertyDescriptorCollection pdcs = TypeDescriptor.GetProperties(th1); 71                     foreach (PropertyDescriptor pd in pdcs) 72                     { 73                         bool bIgnore = false; 74                         // 检查是否有“比较忽略”属性,有则跳过检查 75                         foreach (Attribute at in pd.Attributes) 76                         { 77                             if (at is CompareIgnoreAttribute) 78                             { 79                                 bIgnore = true; 80                                 break; 81                             } 82                         } 83  84                         if (!bIgnore) 85                         { 86                             object ob1 = pd.GetValue(th1); 87                             object ob2 = pd.GetValue(th2); 88                             if (!DataInfo.Equ(ob1, ob2)) 89                             { 90                                 return false; 91                             } 92                         } 93                     } 94                 } 95             } 96             catch (Exception) 97             { 98                 // 不应有异常 99             }100 101             return true;102         }103 104         //以下的方法仅给子类使用105         public virtual DataInfo copy()106         {107             return DataInfo.copy(this) as DataInfo;108         }109         public virtual bool Equ(DataInfo th)110         {111             return DataInfo.Equ(this, th);112         }113     };

 

使用示例

 

 1     [Serializable] // 深拷贝需要 2     public class Car : DataInfo 3     { 4         public string m_name; 5         public string Name 6         { 7             get { return m_name; } 8             set { m_name = value; } 9         }10 11         //[CompareIgnore] 加在这边没用12         protected double m_price;13         [CompareIgnore]14         public double Price15         {16             get { return m_price; }17             set { m_price = value; }18         }19 20         public Car(string name, double price)21         {22             this.m_name = name;23             this.m_price = price;24         }25     }

 

 进行深拷贝

 

1     class Program2     {3         static void Main(string[] args)4         {5             Car a = new Car("a", 199999.99);6             Car b = a.copy() as Car;7             Console.WriteLine("Name " + b.Name.ToString() + ";Price " + b.Price.ToString());8         }9     }

输出结果为:Name a;Price 199999.99

 

 进行比较

 1     class Program 2     { 3         static void Main(string[] args) 4         { 5             Car a = new Car("a", 99999.99); 6             Car b = new Car("a", 199999.99); 7             bool bSame = a.Equ(b); 8             Console.WriteLine(bSame.ToString()); 9         }10     }

输出结果为:True