2 using System.Reflection;
4 namespace Checker_ABI.Utilities
6 /// -----------------------------------------------------------------------
8 /// This utility class contains a rich sets of utility methods that perform operations
9 /// on objects during runtime such as copying of property and field values
10 /// between 2 objects, deep cloning of objects, etc.
12 /// -----------------------------------------------------------------------
13 public abstract class ObjectUtils
16 /// Deep Comparison two objects if they are alike. The objects are consider alike if
18 /// <list type="ordered">
19 /// <item>of the same <see cref="System.Type"/>,</item>
20 /// <item>have the same number of methods, properties and fields</item>
21 /// <item>the public and private properties and fields values reflect each other's. </item>
24 /// <param name="original"></param>
25 /// <param name="comparedToObject"></param>
26 /// <returns></returns>
27 public static bool IsALike(object original, object comparedToObject)
30 if (original.GetType() != comparedToObject.GetType())
33 // ...............................................
34 // Compare Number of Private and public Methods
35 // ...............................................
36 MethodInfo[] originalMethods = original
38 .GetMethods(BindingFlags.Instance |
39 BindingFlags.NonPublic |
42 MethodInfo[] comparedMethods = comparedToObject
44 .GetMethods(BindingFlags.Instance |
45 BindingFlags.NonPublic |
48 if (comparedMethods.Length != originalMethods.Length)
51 // ...............................................
52 // Compare Number of Private and public Properties
53 // ................................................
54 PropertyInfo[] originalProperties = original
56 .GetProperties(BindingFlags.Instance |
57 BindingFlags.NonPublic |
60 PropertyInfo[] comparedProperties = comparedToObject
62 .GetProperties(BindingFlags.Instance |
63 BindingFlags.NonPublic |
67 if (comparedProperties.Length != originalProperties.Length)
71 // ...........................................
72 // Compare number of public and private fields
73 // ...........................................
74 FieldInfo[] originalFields = original
76 .GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
78 FieldInfo[] comparedToFields = comparedToObject
80 .GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
83 if (comparedToFields.Length != originalFields.Length)
86 // ........................................
87 // compare field values
88 // ........................................
89 foreach (FieldInfo fi in originalFields)
92 // check to see if the object to contains the field
93 FieldInfo fiComparedObj = comparedToObject.GetType().GetField(fi.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
95 if (fiComparedObj == null)
98 // Get the value of the field from the original object
99 Object srcValue = original.GetType().InvokeMember(fi.Name,
100 BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
107 // Get the value of the field
108 object comparedObjFieldValue = comparedToObject
110 .InvokeMember(fiComparedObj.Name,
111 BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
117 // -------------------------------
118 // now compare the field values
119 // -------------------------------
121 if (srcValue == null)
123 if (comparedObjFieldValue != null)
129 if (srcValue.GetType() != comparedObjFieldValue.GetType())
132 if (!srcValue.ToString().Equals(comparedObjFieldValue.ToString()))
136 // ........................................
137 // compare each Property values
138 // ........................................
139 foreach (PropertyInfo pi in originalProperties)
142 // check to see if the object to contains the field
143 PropertyInfo piComparedObj = comparedToObject
145 .GetProperty(pi.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
147 if (piComparedObj == null)
150 // Get the value of the property from the original object
151 Object srcValue = original
153 .InvokeMember(pi.Name,
154 BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
159 // Get the value of the property
160 object comparedObjValue = comparedToObject
162 .InvokeMember(piComparedObj.Name,
163 BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
169 // -------------------------------
170 // now compare the property values
171 // -------------------------------
172 if (srcValue.GetType() != comparedObjValue.GetType())
175 if (!srcValue.ToString().Equals(comparedObjValue.ToString()))