forked from chuongmep/CadPythonShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectCompare.cs
More file actions
380 lines (328 loc) · 12.4 KB
/
ObjectCompare.cs
File metadata and controls
380 lines (328 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// (C) Copyright 2004 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections;
using System.Reflection;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcadApp = Autodesk.AutoCAD.ApplicationServices;
namespace MgdDbg.Utils
{
/// <summary>
/// This class takes two objects and compares their
/// properties using Reflection and stores them in a map.
/// </summary>
public class ObjectCompare
{
// member vars
private Object m_obj1;
private Object m_obj2;
private PropertyInfo[] m_propInfos1 = null;
private PropertyInfo[] m_propInfos2 = null;
private ArrayList m_commonHierarchy = new ArrayList();
/// <summary>
/// only 2 objects to compare , so restrict the capacity
/// </summary>
private ArrayList m_objData = new ArrayList(2);
private Hashtable m_propMapItem = new Hashtable();
private Hashtable m_propMap = new Hashtable();
/// <summary>
///
/// </summary>
/// <param name="obj1"></param>
/// <param name="obj2"></param>
public
ObjectCompare (Object obj1, Object obj2)
{
if (obj1 == null)
m_obj1 = System.DBNull.Value;
if (obj2 == null)
m_obj2 = System.DBNull.Value;
m_obj1 = obj1;
m_obj2 = obj2;
Initialise();
}
/// <summary>
///
/// </summary>
public ArrayList
CommonHierarchy
{
get { return m_commonHierarchy; }
}
/// <summary>
///
/// </summary>
public void
Initialise ()
{
m_propInfos1 = m_obj1.GetType().GetProperties();
m_propInfos2 = m_obj2.GetType().GetProperties();
Type intersectionType = GetIntersectionLevel(m_obj1, m_obj2);
Boolean isIntersecting = (intersectionType == typeof(System.Object)) ? false : true;
m_propInfos1 = GetPropsTillLevel(m_propInfos1, intersectionType);
m_propInfos2 = GetPropsTillLevel(m_propInfos2, intersectionType);
BuildHierarchy(m_propInfos1);
if (m_obj1.GetType() == typeof(System.DBNull) && m_obj2.GetType() != typeof(System.DBNull)) {
m_propInfos1 = new PropertyInfo[m_propInfos2.Length];
m_propInfos1.Initialize();
BuildHierarchy(m_propInfos2);
}
if (m_obj2.GetType() == typeof(System.DBNull) && m_obj1.GetType() != typeof(System.DBNull)) {
m_propInfos2 = new PropertyInfo[m_propInfos1.Length];
m_propInfos2.Initialize();
BuildHierarchy(m_propInfos1);
}
if (isIntersecting) {
for (int i = m_propInfos1.Length - 1; i >= 0; i--) {
for (int j = m_propInfos2.Length - 1; j >= 0; j--) {
/// Case where a base property is hidden by a derived property
/// ex: Curve's Closed property is hidden by the Polyline's
/// Closed property and thus for a pline object the Closed property
/// doesnt show up at the Curve level, so check to see if the
/// property name *and* the declaring type are the same
if (m_propInfos1[i].Name == m_propInfos2[j].Name
&& m_propInfos1[i].DeclaringType == m_propInfos2[j].DeclaringType) {
PopulateMap(m_propInfos1[i], m_propInfos2[j]);
break;
}
}
}
}
else {
/// Case where a valid object is being compared to a
/// null object. Happens in case of collection objects
for (int i = m_propInfos1.Length - 1; i >= 0; i--) {
PopulateMap(m_propInfos1[i], m_propInfos2[i]);
}
}
/// address collection objects
if (IsCollectionObject(m_obj1) || IsCollectionObject(m_obj2)) {
ArrayList items1 = GetItemsFromCollection(m_obj1);
ArrayList items2 = GetItemsFromCollection(m_obj2);
int maxCount = (items1.Count >= items2.Count) ? items1.Count : items2.Count;
for (int i = 0; i < maxCount; i++) {
m_objData = new ArrayList(2);
try {
m_objData.Add(items1[i]);
}
catch {
m_objData.Add(System.DBNull.Value);
}
try {
m_objData.Add(items2[i]);
}
catch {
m_objData.Add(System.DBNull.Value);
}
string keyStr = String.Format("Item {0}", i);
m_propMapItem[keyStr] = m_objData;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="propInfo1"></param>
/// <param name="propInfo2"></param>
private void
PopulateMap (PropertyInfo propInfo1, PropertyInfo propInfo2)
{
Type declaringType = null;
String propName = string.Empty;
if (propInfo1 != null && propInfo2 != null) {
declaringType = propInfo1.DeclaringType;
propName = propInfo1.Name;
}
if (propInfo1 == null) {
declaringType = propInfo2.DeclaringType;
propName = propInfo2.Name;
}
if (propInfo2 == null) {
declaringType = propInfo1.DeclaringType;
propName = propInfo1.Name;
}
if (!m_propMap.ContainsKey(declaringType)) {
m_propMapItem = new Hashtable();
m_propMap.Add(declaringType, m_propMapItem);
}
m_objData = new ArrayList(2);
/// some properties throw an exception
/// handle them
try {
object value = propInfo1.GetValue(m_obj1, null);
if (value == null)
m_objData.Add(System.DBNull.Value);
else
m_objData.Add(value);
}
catch {
m_objData.Add(System.DBNull.Value);
}
/// some properties throw an exception
/// handle them
try {
object value = propInfo2.GetValue(m_obj2, null);
if (value == null)
m_objData.Add(System.DBNull.Value);
else
m_objData.Add(value);
}
catch {
m_objData.Add(System.DBNull.Value);
}
m_propMapItem[propName] = m_objData;
}
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public Hashtable
GetPropsAtLevel (Type type)
{
return m_propMap[type] as Hashtable;
}
/// <summary>
/// Build and store the object hierarchy
/// </summary>
/// <param name="propInfos"></param>
private void
BuildHierarchy (PropertyInfo[] propInfos)
{
Type type = null;
Stack types = new Stack();
foreach (PropertyInfo propInfo in propInfos) {
if (propInfo.DeclaringType != type) {
type = propInfo.DeclaringType;
types.Push(type);
}
}
m_commonHierarchy.Clear();
while (types.Count != 0) {
m_commonHierarchy.Add(types.Pop() as Type);
}
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private Boolean
IsCollectionObject (Object obj)
{
/// a collection object has to implement IList
if (obj.GetType().GetInterface(typeof(IList).FullName) != null)
return true;
return false;
}
/// <summary>
/// Gets the properties till the level type
/// </summary>
/// <param name="propInfo"></param>
/// <param name="level"></param>
/// <returns></returns>
private PropertyInfo[]
GetPropsTillLevel (PropertyInfo[] propInfo, Type level)
{
Boolean done = false;
for (int i = propInfo.Length - 1; i >= 0; i--) {
if (propInfo[i].DeclaringType == level) {
done = true;
}
else {
if (done) {
PropertyInfo[] tempPropInfo = propInfo;
propInfo = new PropertyInfo[(propInfo.Length) - (i + 1)];
Array.Copy(tempPropInfo, i + 1, propInfo, 0, propInfo.Length);
break;
}
}
}
return propInfo;
}
/// <summary>
/// At what level of the hierarchy do
/// the 2 objects intersect
/// </summary>
/// <param name="obj1"></param>
/// <param name="obj2"></param>
/// <returns></returns>
private Type
GetIntersectionLevel (Object obj1, Object obj2)
{
Type type1 = obj1.GetType();
Type type2 = obj2.GetType();
Type origType2 = obj2.GetType();
while (type1 != type2) {
if (type2 != null)
type2 = type2.BaseType;
else {
type2 = origType2;
type1 = type1.BaseType;
}
}
return type1;
}
/// <summary>
/// mine into collection objects and get their items
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private ArrayList
GetItemsFromCollection (Object obj)
{
PropertyInfo[] propInfos = obj.GetType().GetProperties();
PropertyInfo propInfoItem = null;
Int32 count = 0;
foreach (PropertyInfo propInfo in propInfos) {
ParameterInfo[] paramArr = propInfo.GetIndexParameters();
if (paramArr.Length != 0) {
propInfoItem = propInfo;
continue;
}
/// TBD: an IList implementer has to implement
/// the "Count" property, so this should be safe
/// albeit there should be a better way
if (propInfo.Name == "Count") {
count = (Int32)propInfo.GetValue(obj, null);
}
}
ArrayList itemData = new ArrayList();
/// some properties throw an exception
/// handle them
for (int i = 0; i < count; i++) {
object[] index = new object[1];
index[0] = i;
try {
object value = propInfoItem.GetValue(obj, index);
if (value == null)
itemData.Add(System.DBNull.Value);
else
itemData.Add(value);
}
catch {
itemData.Add(System.DBNull.Value);
}
}
return itemData;
}
}
}