CloneUtil.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace MECF.Framework.Common.Utilities
  10. {
  11. public sealed class CloneUtil
  12. {
  13. public static bool CheckIsChanged<T>(T OldValue, T NewValue)
  14. {
  15. Type typeSource = OldValue.GetType();
  16. PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  17. Type typeNewSource = NewValue.GetType();
  18. PropertyInfo[] newPropertyInfo = typeNewSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  19. foreach (PropertyInfo property in propertyInfo)
  20. {
  21. //Check whether property can be written to
  22. if (!property.CanWrite) continue;
  23. if (property.Name == "IsNotifying") continue;
  24. //check whether property type is value type, enum or string type
  25. if (property.PropertyType.IsPrimitive || property.PropertyType.IsEnum)
  26. {
  27. var newProperty = newPropertyInfo.Where(x => x.Name == property.Name).FirstOrDefault();
  28. if (property.GetValue(OldValue, null) != newProperty.GetValue(NewValue, null))
  29. {
  30. return false;
  31. }
  32. }
  33. else if (property.PropertyType == typeof(string))
  34. {
  35. var oldPropertyValue = property.GetValue(OldValue, null) == null ? "" : property.GetValue(OldValue, null);
  36. var newProperty = newPropertyInfo.Where(x => x.Name == property.Name).FirstOrDefault();
  37. var newPropertyValue = newProperty.GetValue(NewValue, null) == null ? "" : newProperty.GetValue(NewValue, null);
  38. if (oldPropertyValue != newPropertyValue)
  39. {
  40. return false;
  41. }
  42. }
  43. }
  44. return true;
  45. }
  46. public static object CloneObject(object objSource)
  47. {
  48. //Get the type of source object and create a new instance of that type
  49. Type typeSource = objSource.GetType();
  50. object objTarget = Activator.CreateInstance(typeSource);
  51. //Get all the properties of source object type
  52. PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  53. //Assign all source property to taget object 's properties
  54. foreach (PropertyInfo property in propertyInfo)
  55. {
  56. //Check whether property can be written to
  57. if (!property.CanWrite) continue;
  58. if (property.Name == "StepNo") continue;
  59. //check whether property type is value type, enum or string type
  60. if (property.PropertyType.IsPrimitive || property.PropertyType.IsEnum || property.PropertyType == typeof(string))
  61. {
  62. property.SetValue(objTarget, property.GetValue(objSource, null), null);
  63. }
  64. else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
  65. {
  66. //Include List and Dictionary and......
  67. if (property.PropertyType.IsGenericType)
  68. {
  69. var cloneObj = Activator.CreateInstance(property.PropertyType);
  70. var addMethod = property.PropertyType.GetMethod("Add", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  71. Debug.Assert(null != addMethod);
  72. var currentValues = property.GetValue(objSource, null) as IEnumerable;
  73. if (currentValues == null)
  74. {
  75. property.SetValue(objTarget, null, null);
  76. }
  77. else
  78. {
  79. if (cloneObj is IDictionary)
  80. {
  81. cloneObj = cloneObj as IDictionary;
  82. foreach (var currentValue in currentValues)
  83. {
  84. var propInfoKey = currentValue.GetType().GetProperty("Key");
  85. var propInfoValue = currentValue.GetType().GetProperty("Value");
  86. if (propInfoKey != null && propInfoValue != null)
  87. {
  88. var keyValue = propInfoKey.GetValue(currentValue, null);
  89. var valueValue = propInfoValue.GetValue(currentValue, null);
  90. object finalKeyValue, finalValueValue;
  91. //Get finalKeyValue
  92. var currentKeyType = keyValue.GetType();
  93. if (currentKeyType.IsPrimitive || currentKeyType.IsEnum || currentKeyType == typeof(string))
  94. {
  95. finalKeyValue = keyValue;
  96. }
  97. else
  98. {
  99. //Reference type
  100. var copyObj = CloneObject(keyValue);
  101. finalKeyValue = copyObj;
  102. }
  103. //Get finalValueValue
  104. var currentValueType = valueValue.GetType();
  105. if (currentValueType.IsPrimitive || currentValueType.IsEnum || currentValueType == typeof(string))
  106. {
  107. finalValueValue = valueValue;
  108. }
  109. else
  110. {
  111. //Reference type
  112. var copyObj = CloneObject(valueValue);
  113. finalValueValue = copyObj;
  114. }
  115. addMethod.Invoke(cloneObj, new[] { finalKeyValue, finalValueValue });
  116. }
  117. }
  118. property.SetValue(objTarget, cloneObj, null);
  119. }
  120. //Common IList type
  121. else
  122. {
  123. foreach (var currentValue in currentValues)
  124. {
  125. var currentType = currentValue.GetType();
  126. if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))
  127. {
  128. addMethod.Invoke(cloneObj, new[] { currentValue });
  129. }
  130. else
  131. {
  132. //Reference type
  133. var copyObj = CloneObject(currentValue);
  134. addMethod.Invoke(cloneObj, new[] { copyObj });
  135. }
  136. }
  137. property.SetValue(objTarget, cloneObj, null);
  138. }
  139. }
  140. }
  141. //Array type
  142. else
  143. {
  144. var currentValues = property.GetValue(objSource, null) as Array;
  145. if (null == currentValues)
  146. {
  147. property.SetValue(objTarget, null, null);
  148. }
  149. else
  150. {
  151. var cloneObj = Activator.CreateInstance(property.PropertyType, currentValues.Length) as Array;
  152. var arrayList = new ArrayList();
  153. for (var i = 0; i < currentValues.Length; i++)
  154. {
  155. var currentType = currentValues.GetValue(i).GetType();
  156. if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))
  157. {
  158. arrayList.Add(currentValues.GetValue(i));
  159. }
  160. else
  161. {
  162. //Reference type
  163. var copyObj = CloneObject(currentValues.GetValue(i));
  164. arrayList.Add(copyObj);
  165. }
  166. }
  167. arrayList.CopyTo(cloneObj, 0);
  168. property.SetValue(objTarget, cloneObj, null);
  169. }
  170. }
  171. }
  172. //else property type is object/complex types, so need to recursively call this method until the end of the tree is reached
  173. else
  174. {
  175. object objPropertyValue = property.GetValue(objSource, null);
  176. if (objPropertyValue == null)
  177. {
  178. property.SetValue(objTarget, null, null);
  179. }
  180. else if (objPropertyValue.GetType().IsPrimitive || objPropertyValue.GetType().IsEnum || objPropertyValue.GetType() == typeof(string))
  181. {
  182. property.SetValue(objTarget, objPropertyValue, null);
  183. }
  184. else
  185. {
  186. property.SetValue(objTarget, CloneObject(objPropertyValue), null);
  187. }
  188. }
  189. }
  190. return objTarget;
  191. }
  192. public static object CloneObjectSetFlag(object objSource, string flag, bool setValue)
  193. {
  194. //Get the type of source object and create a new instance of that type
  195. Type typeSource = objSource.GetType();
  196. object objTarget = Activator.CreateInstance(typeSource);
  197. //Get all the properties of source object type
  198. PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  199. //Assign all source property to taget object 's properties
  200. foreach (PropertyInfo property in propertyInfo)
  201. {
  202. //Check whether property can be written to
  203. if (!property.CanWrite) continue;
  204. if (property.Name == "StepNo") continue;
  205. //check whether property type is value type, enum or string type
  206. if (property.PropertyType.IsPrimitive || property.PropertyType.IsEnum || property.PropertyType == typeof(string))
  207. {
  208. if (property.Name.Contains(flag))
  209. { }
  210. else
  211. {
  212. property.SetValue(objTarget, property.GetValue(objSource, null), null);
  213. }
  214. }
  215. else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
  216. {
  217. //Include List and Dictionary and......
  218. if (property.PropertyType.IsGenericType)
  219. {
  220. var cloneObj = Activator.CreateInstance(property.PropertyType);
  221. var addMethod = property.PropertyType.GetMethod("Add", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  222. Debug.Assert(null != addMethod);
  223. var currentValues = property.GetValue(objSource, null) as IEnumerable;
  224. if (currentValues == null)
  225. {
  226. property.SetValue(objTarget, null, null);
  227. }
  228. else
  229. {
  230. if (cloneObj is IDictionary)
  231. {
  232. cloneObj = cloneObj as IDictionary;
  233. foreach (var currentValue in currentValues)
  234. {
  235. var propInfoKey = currentValue.GetType().GetProperty("Key");
  236. var propInfoValue = currentValue.GetType().GetProperty("Value");
  237. if (propInfoKey != null && propInfoValue != null)
  238. {
  239. var keyValue = propInfoKey.GetValue(currentValue, null);
  240. var valueValue = propInfoValue.GetValue(currentValue, null);
  241. object finalKeyValue, finalValueValue;
  242. //Get finalKeyValue
  243. var currentKeyType = keyValue.GetType();
  244. if (currentKeyType.IsPrimitive || currentKeyType.IsEnum || currentKeyType == typeof(string))
  245. {
  246. finalKeyValue = keyValue;
  247. }
  248. else
  249. {
  250. //Reference type
  251. var copyObj = CloneObjectSetFlag(keyValue, flag, setValue);
  252. finalKeyValue = copyObj;
  253. }
  254. //Get finalValueValue
  255. var currentValueType = valueValue.GetType();
  256. if (currentValueType.IsPrimitive || currentValueType.IsEnum || currentValueType == typeof(string))
  257. {
  258. finalValueValue = valueValue;
  259. }
  260. else
  261. {
  262. //Reference type
  263. var copyObj = CloneObjectSetFlag(valueValue, flag, setValue);
  264. finalValueValue = copyObj;
  265. }
  266. addMethod.Invoke(cloneObj, new[] { finalKeyValue, finalValueValue });
  267. }
  268. }
  269. property.SetValue(objTarget, cloneObj, null);
  270. }
  271. //Common IList type
  272. else
  273. {
  274. foreach (var currentValue in currentValues)
  275. {
  276. var currentType = currentValue.GetType();
  277. if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))
  278. {
  279. addMethod.Invoke(cloneObj, new[] { currentValue });
  280. }
  281. else
  282. {
  283. //Reference type
  284. var copyObj = CloneObjectSetFlag(currentValue, flag, setValue);
  285. addMethod.Invoke(cloneObj, new[] { copyObj });
  286. }
  287. }
  288. property.SetValue(objTarget, cloneObj, null);
  289. }
  290. }
  291. }
  292. //Array type
  293. else
  294. {
  295. var currentValues = property.GetValue(objSource, null) as Array;
  296. if (null == currentValues)
  297. {
  298. property.SetValue(objTarget, null, null);
  299. }
  300. else
  301. {
  302. var cloneObj = Activator.CreateInstance(property.PropertyType, currentValues.Length) as Array;
  303. var arrayList = new ArrayList();
  304. for (var i = 0; i < currentValues.Length; i++)
  305. {
  306. var currentType = currentValues.GetValue(i).GetType();
  307. if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))
  308. {
  309. arrayList.Add(currentValues.GetValue(i));
  310. }
  311. else
  312. {
  313. //Reference type
  314. var copyObj = CloneObjectSetFlag(currentValues.GetValue(i), flag, setValue);
  315. arrayList.Add(copyObj);
  316. }
  317. }
  318. arrayList.CopyTo(cloneObj, 0);
  319. property.SetValue(objTarget, cloneObj, null);
  320. }
  321. }
  322. }
  323. //else property type is object/complex types, so need to recursively call this method until the end of the tree is reached
  324. else
  325. {
  326. object objPropertyValue = property.GetValue(objSource, null);
  327. if (objPropertyValue == null)
  328. {
  329. property.SetValue(objTarget, null, null);
  330. }
  331. else if (objPropertyValue.GetType().IsPrimitive || objPropertyValue.GetType().IsEnum || objPropertyValue.GetType() == typeof(string))
  332. {
  333. property.SetValue(objTarget, objPropertyValue, null);
  334. }
  335. else
  336. {
  337. property.SetValue(objTarget, CloneObjectSetFlag(objPropertyValue, flag, setValue), null);
  338. }
  339. }
  340. }
  341. return objTarget;
  342. }
  343. public static object CloneAllObject(object objSource)
  344. {
  345. //Get the type of source object and create a new instance of that type
  346. Type typeSource = objSource.GetType();
  347. object objTarget = Activator.CreateInstance(typeSource);
  348. //Get all the properties of source object type
  349. PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  350. //Assign all source property to taget object 's properties
  351. foreach (PropertyInfo property in propertyInfo)
  352. {
  353. //Check whether property can be written to
  354. if (!property.CanWrite) continue;
  355. //check whether property type is value type, enum or string type
  356. if (property.PropertyType.IsPrimitive || property.PropertyType.IsEnum || property.PropertyType == typeof(string))
  357. {
  358. property.SetValue(objTarget, property.GetValue(objSource, null), null);
  359. }
  360. else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
  361. {
  362. //Include List and Dictionary and......
  363. if (property.PropertyType.IsGenericType)
  364. {
  365. var cloneObj = Activator.CreateInstance(property.PropertyType);
  366. var addMethod = property.PropertyType.GetMethod("Add", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  367. Debug.Assert(null != addMethod);
  368. var currentValues = property.GetValue(objSource, null) as IEnumerable;
  369. if (currentValues == null)
  370. {
  371. property.SetValue(objTarget, null, null);
  372. }
  373. else
  374. {
  375. if (cloneObj is IDictionary)
  376. {
  377. cloneObj = cloneObj as IDictionary;
  378. foreach (var currentValue in currentValues)
  379. {
  380. var propInfoKey = currentValue.GetType().GetProperty("Key");
  381. var propInfoValue = currentValue.GetType().GetProperty("Value");
  382. if (propInfoKey != null && propInfoValue != null)
  383. {
  384. var keyValue = propInfoKey.GetValue(currentValue, null);
  385. var valueValue = propInfoValue.GetValue(currentValue, null);
  386. object finalKeyValue, finalValueValue;
  387. //Get finalKeyValue
  388. var currentKeyType = keyValue.GetType();
  389. if (currentKeyType.IsPrimitive || currentKeyType.IsEnum || currentKeyType == typeof(string))
  390. {
  391. finalKeyValue = keyValue;
  392. }
  393. else
  394. {
  395. //Reference type
  396. var copyObj = CloneObject(keyValue);
  397. finalKeyValue = copyObj;
  398. }
  399. //Get finalValueValue
  400. var currentValueType = valueValue.GetType();
  401. if (currentValueType.IsPrimitive || currentValueType.IsEnum || currentValueType == typeof(string))
  402. {
  403. finalValueValue = valueValue;
  404. }
  405. else
  406. {
  407. //Reference type
  408. var copyObj = CloneObject(valueValue);
  409. finalValueValue = copyObj;
  410. }
  411. addMethod.Invoke(cloneObj, new[] { finalKeyValue, finalValueValue });
  412. }
  413. }
  414. property.SetValue(objTarget, cloneObj, null);
  415. }
  416. //Common IList type
  417. else
  418. {
  419. foreach (var currentValue in currentValues)
  420. {
  421. var currentType = currentValue.GetType();
  422. if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))
  423. {
  424. addMethod.Invoke(cloneObj, new[] { currentValue });
  425. }
  426. else
  427. {
  428. //Reference type
  429. var copyObj = CloneObject(currentValue);
  430. addMethod.Invoke(cloneObj, new[] { copyObj });
  431. }
  432. }
  433. property.SetValue(objTarget, cloneObj, null);
  434. }
  435. }
  436. }
  437. //Array type
  438. else
  439. {
  440. var currentValues = property.GetValue(objSource, null) as Array;
  441. if (null == currentValues)
  442. {
  443. property.SetValue(objTarget, null, null);
  444. }
  445. else
  446. {
  447. var cloneObj = Activator.CreateInstance(property.PropertyType, currentValues.Length) as Array;
  448. var arrayList = new ArrayList();
  449. for (var i = 0; i < currentValues.Length; i++)
  450. {
  451. var currentType = currentValues.GetValue(i).GetType();
  452. if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))
  453. {
  454. arrayList.Add(currentValues.GetValue(i));
  455. }
  456. else
  457. {
  458. //Reference type
  459. var copyObj = CloneObject(currentValues.GetValue(i));
  460. arrayList.Add(copyObj);
  461. }
  462. }
  463. arrayList.CopyTo(cloneObj, 0);
  464. property.SetValue(objTarget, cloneObj, null);
  465. }
  466. }
  467. }
  468. //else property type is object/complex types, so need to recursively call this method until the end of the tree is reached
  469. else
  470. {
  471. object objPropertyValue = property.GetValue(objSource, null);
  472. if (objPropertyValue == null)
  473. {
  474. property.SetValue(objTarget, null, null);
  475. }
  476. else if (objPropertyValue.GetType().IsPrimitive || objPropertyValue.GetType().IsEnum || objPropertyValue.GetType() == typeof(string))
  477. {
  478. property.SetValue(objTarget, objPropertyValue, null);
  479. }
  480. else
  481. {
  482. property.SetValue(objTarget, CloneObject(objPropertyValue), null);
  483. }
  484. }
  485. }
  486. return objTarget;
  487. }
  488. }
  489. }