ObjectExtensions.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /// <summary>
  2. /// Object extensions.
  3. /// This is a 3rd person class from:
  4. /// https://github.com/Burtsev-Alexey/net-object-deep-copy/blob/master/ObjectExtensions.cs
  5. /// </summary>
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. using System.ArrayExtensions;
  9. namespace System
  10. {
  11. public static class ObjectExtensions
  12. {
  13. private static readonly MethodInfo CloneMethod = typeof(Object).GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance);
  14. public static bool IsPrimitive(this Type type)
  15. {
  16. if (type == typeof(String)) return true;
  17. return (type.IsValueType & type.IsPrimitive);
  18. }
  19. public static Object Copy(this Object originalObject)
  20. {
  21. return InternalCopy(originalObject, new Dictionary<Object, Object>(new ReferenceEqualityComparer()));
  22. }
  23. private static Object InternalCopy(Object originalObject, IDictionary<Object, Object> visited)
  24. {
  25. if (originalObject == null) return null;
  26. var typeToReflect = originalObject.GetType();
  27. if (IsPrimitive(typeToReflect)) return originalObject;
  28. if (visited.ContainsKey(originalObject)) return visited[originalObject];
  29. if (typeof(Delegate).IsAssignableFrom(typeToReflect)) return null;
  30. var cloneObject = CloneMethod.Invoke(originalObject, null);
  31. if (typeToReflect.IsArray)
  32. {
  33. var arrayType = typeToReflect.GetElementType();
  34. if (IsPrimitive(arrayType) == false)
  35. {
  36. Array clonedArray = (Array)cloneObject;
  37. clonedArray.ForEach((array, indices) => {
  38. array.SetValue(InternalCopy(clonedArray.GetValue(indices), visited), indices);
  39. });
  40. }
  41. }
  42. visited.Add(originalObject, cloneObject);
  43. CopyFields(originalObject, visited, cloneObject, typeToReflect);
  44. RecursiveCopyBaseTypePrivateFields(originalObject, visited, cloneObject, typeToReflect);
  45. return cloneObject;
  46. }
  47. private static void RecursiveCopyBaseTypePrivateFields(object originalObject, IDictionary<object, object> visited, object cloneObject, Type typeToReflect)
  48. {
  49. if (typeToReflect.BaseType != null)
  50. {
  51. RecursiveCopyBaseTypePrivateFields(originalObject, visited, cloneObject, typeToReflect.BaseType);
  52. CopyFields(originalObject, visited, cloneObject, typeToReflect.BaseType, BindingFlags.Instance | BindingFlags.NonPublic, info => info.IsPrivate);
  53. }
  54. }
  55. private static void CopyFields(object originalObject, IDictionary<object, object> visited, object cloneObject, Type typeToReflect, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy, Func<FieldInfo, bool> filter = null)
  56. {
  57. foreach (FieldInfo fieldInfo in typeToReflect.GetFields(bindingFlags))
  58. {
  59. if (filter != null && filter(fieldInfo) == false) continue;
  60. if (IsPrimitive(fieldInfo.FieldType)) continue;
  61. var originalFieldValue = fieldInfo.GetValue(originalObject);
  62. var clonedFieldValue = InternalCopy(originalFieldValue, visited);
  63. fieldInfo.SetValue(cloneObject, clonedFieldValue);
  64. }
  65. }
  66. public static T Copy<T>(this T original)
  67. {
  68. return (T)Copy((Object)original);
  69. }
  70. }
  71. public class ReferenceEqualityComparer : EqualityComparer<Object>
  72. {
  73. public override bool Equals(object x, object y)
  74. {
  75. return ReferenceEquals(x, y);
  76. }
  77. public override int GetHashCode(object obj)
  78. {
  79. if (obj == null) return 0;
  80. return obj.GetHashCode();
  81. }
  82. }
  83. namespace ArrayExtensions
  84. {
  85. public static class ArrayExtensions
  86. {
  87. public static void ForEach(this Array array, Action<Array, int[]> action)
  88. {
  89. if (array.LongLength == 0) return;
  90. ArrayTraverse walker = new ArrayTraverse(array);
  91. do action(array, walker.Position);
  92. while (walker.Step());
  93. }
  94. }
  95. internal class ArrayTraverse
  96. {
  97. public int[] Position;
  98. private int[] maxLengths;
  99. public ArrayTraverse(Array array)
  100. {
  101. maxLengths = new int[array.Rank];
  102. for (int i = 0; i < array.Rank; ++i)
  103. {
  104. maxLengths[i] = array.GetLength(i) - 1;
  105. }
  106. Position = new int[array.Rank];
  107. }
  108. public bool Step()
  109. {
  110. for (int i = 0; i < Position.Length; ++i)
  111. {
  112. if (Position[i] < maxLengths[i])
  113. {
  114. Position[i]++;
  115. for (int j = 0; j < i; j++)
  116. {
  117. Position[j] = 0;
  118. }
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. }
  125. }
  126. }