| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Reflection; |
| | 4 | |
|
| | 5 | | namespace FakeXrmEasy.Extensions |
| | 6 | | { |
| | 7 | | //taken from: |
| | 8 | | //https://github.com/Burtsev-Alexey/net-object-deep-copy/blob/master/ObjectExtensions.cs |
| | 9 | | /// <summary> |
| | 10 | | /// Deep cloning of the object |
| | 11 | | /// </summary> |
| | 12 | | public static class ObjectExtensions |
| | 13 | | { |
| 6 | 14 | | private static readonly MethodInfo CloneMethod = typeof(Object).GetMethod("MemberwiseClone", BindingFlags.NonPub |
| | 15 | |
|
| | 16 | | public static bool IsPrimitive(this Type type) |
| 8934684 | 17 | | { |
| 10843775 | 18 | | if (type == typeof(String)) return true; |
| 7025593 | 19 | | return (type.IsValueType & type.IsPrimitive); |
| 8934684 | 20 | | } |
| | 21 | |
|
| | 22 | | private static FieldInfo GetFieldInfo(Type type, string fieldName) |
| 172018 | 23 | | { |
| | 24 | | FieldInfo fieldInfo; |
| | 25 | | do |
| 345854 | 26 | | { |
| 345854 | 27 | | fieldInfo = type.GetField(fieldName, |
| 345854 | 28 | | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); |
| 345854 | 29 | | type = type.BaseType; |
| 345854 | 30 | | } |
| 345854 | 31 | | while (fieldInfo == null && type != null); |
| 172018 | 32 | | return fieldInfo; |
| 172018 | 33 | | } |
| | 34 | |
|
| | 35 | | public static object GetFieldValue(this object obj, string fieldName) |
| 0 | 36 | | { |
| 0 | 37 | | if (obj == null) |
| 0 | 38 | | throw new ArgumentNullException("obj"); |
| 0 | 39 | | Type objType = obj.GetType(); |
| 0 | 40 | | FieldInfo fieldInfo = GetFieldInfo(objType, fieldName); |
| 0 | 41 | | if (fieldInfo == null) |
| 0 | 42 | | throw new ArgumentOutOfRangeException("fieldName", |
| 0 | 43 | | string.Format("Couldn't find field {0} in type {1}", fieldName, objType.FullName)); |
| 0 | 44 | | return fieldInfo.GetValue(obj); |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public static void SetFieldValue(this object obj, string fieldName, object val) |
| 172018 | 48 | | { |
| 172018 | 49 | | if (obj == null) |
| 0 | 50 | | throw new ArgumentNullException("obj"); |
| 172018 | 51 | | Type objType = obj.GetType(); |
| 172018 | 52 | | FieldInfo fieldInfo = GetFieldInfo(objType, fieldName); |
| 172018 | 53 | | if (fieldInfo == null) |
| 0 | 54 | | throw new ArgumentOutOfRangeException("fieldName", |
| 0 | 55 | | string.Format("Couldn't find field {0} in type {1}", fieldName, objType.FullName)); |
| 172018 | 56 | | fieldInfo.SetValue(obj, val); |
| 172018 | 57 | | } |
| | 58 | |
|
| | 59 | |
|
| | 60 | | public static Object Copy(this Object originalObject) |
| 6325 | 61 | | { |
| 6325 | 62 | | return InternalCopy(originalObject, new Dictionary<Object, Object>(new ReferenceEqualityComparer())); |
| 6325 | 63 | | } |
| | 64 | |
|
| | 65 | | private static Object InternalCopy(Object originalObject, IDictionary<Object, Object> visited) |
| 6250512 | 66 | | { |
| 11886874 | 67 | | if (originalObject == null) return null; |
| 614150 | 68 | | var typeToReflect = originalObject.GetType(); |
| 619964 | 69 | | if (IsPrimitive(typeToReflect)) return originalObject; |
| 691624 | 70 | | if (visited.ContainsKey(originalObject)) return visited[originalObject]; |
| 525048 | 71 | | if (typeof(Delegate).IsAssignableFrom(typeToReflect)) return null; |
| 525048 | 72 | | var cloneObject = CloneMethod.Invoke(originalObject, null); |
| 525048 | 73 | | if (typeToReflect.IsArray) |
| 19561 | 74 | | { |
| 19561 | 75 | | var arrayType = typeToReflect.GetElementType(); |
| 19561 | 76 | | if (IsPrimitive(arrayType) == false) |
| 18058 | 77 | | { |
| 18058 | 78 | | Array clonedArray = (Array)cloneObject; |
| 258018 | 79 | | clonedArray.ForEach((array, indices) => array.SetValue(InternalCopy(clonedArray.GetValue(indices), v |
| 18058 | 80 | | } |
| 19561 | 81 | | } |
| 525048 | 82 | | visited.Add(originalObject, cloneObject); |
| 525048 | 83 | | CopyFields(originalObject, visited, cloneObject, typeToReflect); |
| 525048 | 84 | | RecursiveCopyBaseTypePrivateFields(originalObject, visited, cloneObject, typeToReflect); |
| 525048 | 85 | | return cloneObject; |
| 6250512 | 86 | | } |
| | 87 | |
|
| | 88 | | private static void RecursiveCopyBaseTypePrivateFields(object originalObject, IDictionary<object, object> visite |
| 2010212 | 89 | | { |
| 2010212 | 90 | | if (typeToReflect.BaseType != null) |
| 1485164 | 91 | | { |
| 1485164 | 92 | | RecursiveCopyBaseTypePrivateFields(originalObject, visited, cloneObject, typeToReflect.BaseType); |
| 7630515 | 93 | | CopyFields(originalObject, visited, cloneObject, typeToReflect.BaseType, BindingFlags.Instance | Binding |
| 1485164 | 94 | | } |
| 2010212 | 95 | | } |
| | 96 | |
|
| | 97 | | private static void CopyFields(object originalObject, IDictionary<object, object> visited, object cloneObject, T |
| 2010212 | 98 | | { |
| 22632582 | 99 | | foreach (FieldInfo fieldInfo in typeToReflect.GetFields(bindingFlags)) |
| 8300973 | 100 | | { |
| 8300973 | 101 | | if (filter != null && filter(fieldInfo) == false) continue; |
| 10597719 | 102 | | if (IsPrimitive(fieldInfo.FieldType)) continue; |
| 6004227 | 103 | | var originalFieldValue = fieldInfo.GetValue(originalObject); |
| 6004227 | 104 | | var clonedFieldValue = InternalCopy(originalFieldValue, visited); |
| 6004227 | 105 | | fieldInfo.SetValue(cloneObject, clonedFieldValue); |
| 6004227 | 106 | | } |
| 2010212 | 107 | | } |
| | 108 | |
|
| | 109 | | public static T Copy<T>(this T original) |
| 6325 | 110 | | { |
| 6325 | 111 | | return (T)Copy((Object)original); |
| 6325 | 112 | | } |
| | 113 | | } |
| | 114 | |
|
| | 115 | | public class ReferenceEqualityComparer : EqualityComparer<Object> |
| | 116 | | { |
| | 117 | | public override bool Equals(object x, object y) |
| | 118 | | { |
| | 119 | | return ReferenceEquals(x, y); |
| | 120 | | } |
| | 121 | |
|
| | 122 | | public override int GetHashCode(object obj) |
| | 123 | | { |
| | 124 | | if (obj == null) return 0; |
| | 125 | | return obj.GetHashCode(); |
| | 126 | | } |
| | 127 | | } |
| | 128 | |
|
| | 129 | | public static class ArrayExtensions |
| | 130 | | { |
| | 131 | | public static void ForEach(this Array array, Action<Array, int[]> action) |
| | 132 | | { |
| | 133 | | if (array.LongLength == 0) return; |
| | 134 | | ArrayTraverse walker = new ArrayTraverse(array); |
| | 135 | | do action(array, walker.Position); |
| | 136 | | while (walker.Step()); |
| | 137 | | } |
| | 138 | | } |
| | 139 | |
|
| | 140 | | internal class ArrayTraverse |
| | 141 | | { |
| | 142 | | public int[] Position; |
| | 143 | | private int[] maxLengths; |
| | 144 | |
|
| | 145 | | public ArrayTraverse(Array array) |
| | 146 | | { |
| | 147 | | maxLengths = new int[array.Rank]; |
| | 148 | | for (int i = 0; i < array.Rank; ++i) |
| | 149 | | { |
| | 150 | | maxLengths[i] = array.GetLength(i) - 1; |
| | 151 | | } |
| | 152 | | Position = new int[array.Rank]; |
| | 153 | | } |
| | 154 | |
|
| | 155 | | public bool Step() |
| | 156 | | { |
| | 157 | | for (int i = 0; i < Position.Length; ++i) |
| | 158 | | { |
| | 159 | | if (Position[i] < maxLengths[i]) |
| | 160 | | { |
| | 161 | | Position[i]++; |
| | 162 | | for (int j = 0; j < i; j++) |
| | 163 | | { |
| | 164 | | Position[j] = 0; |
| | 165 | | } |
| | 166 | | return true; |
| | 167 | | } |
| | 168 | | } |
| | 169 | | return false; |
| | 170 | | } |
| | 171 | | } |
| | 172 | | } |