-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCopyBaseHelper.cs
More file actions
204 lines (191 loc) · 7.05 KB
/
CopyBaseHelper.cs
File metadata and controls
204 lines (191 loc) · 7.05 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace EP.Ex
{
/// <summary>
/// Helper of custom object copy
/// </summary>
internal static class CopyBaseHelper
{
#region Internal Fields
internal const BindingFlags FInternalStatic = Obj.FInternalStatic;
internal const BindingFlags FPublicStatic = Obj.FPublicStatic;
#endregion Internal Fields
#region Public Methods
/// <summary>
/// Get delegate func that create deep copy of object
/// </summary>
/// <typeparam name="T">Source object type</typeparam>
/// <returns>delegate if present, or null</returns>
public static Func<T, Dictionary<object, object>, T> DeepCopyFunc<T>()
{
var mi = m_get_info(typeof(T));
return mi != null ? (Func<T, Dictionary<object, object>, T>)Delegate.CreateDelegate(typeof(Func<T, Dictionary<object, object>, T>), mi) : null;
}
/// <summary>
/// Helper that make deep copy of Dictionary
/// </summary>
/// <typeparam name="K">Type of Key property</typeparam>
/// <typeparam name="V">Type of Value property</typeparam>
/// <param name="src">Source dictionary</param>
/// <param name="dict">
/// Dictionary, that accumulate copies of objects, to have one object - one copy
/// </param>
/// <returns>Deep copy of dictionary</returns>
public static Dictionary<K, V> m_deep_copy_dict<K, V>(Dictionary<K, V> src, Dictionary<object, object> dict)
{
var dst = new Dictionary<K, V>(src.Count, src.Comparer);
object key;
object value;
foreach (var p in src)
{
if (!dict.TryGetValue(p.Key, out key))
{
key = Obj.DeepCopy(p.Key, dict);
}
if (p.Value != null)
{
if (!dict.TryGetValue(p.Value, out value))
{
value = Obj.DeepCopy(p.Value, dict);
}
}
else
{
value = p.Value;
}
dst[(K)key] = (V)value;
}
return dst;
}
/// <summary>
/// Helper that make deep copy of hashset
/// </summary>
/// <typeparam name="T">Type of hashset items</typeparam>
/// <param name="src">Source hashset</param>
/// <param name="dic">
/// Dictionary, that accumulate copies of objects, to have one object - one copy
/// </param>
/// <returns>Deep copy of hashset</returns>
public static HashSet<T> m_deep_copy_hashset<T>(HashSet<T> src, Dictionary<object, object> dict)
{
var dst = new HashSet<T>(src.Comparer);
object value;
foreach (var p in src)
{
if (!dict.TryGetValue(p, out value))
{
value = Obj.DeepCopy((object)p, dict);
}
dst.Add((T)value);
}
return dst;
}
/// <summary>
/// Helper that make deep copy of Hashtable
/// </summary>
/// <param name="src">Source Hashtable</param>
/// <param name="dict">
/// Dictionary, that accumulate copies of objects, to have one object - one copy
/// </param>
/// <returns>Deep copy of Hashtable</returns>
public static Hashtable m_deep_copy_hashtable(Hashtable src, Dictionary<object, object> dict)
{
var dst = ((Hashtable)src.Clone());//TODO: rewrite
dst.Clear();
object key;
object value;
foreach (DictionaryEntry p in src)
{
if (!dict.TryGetValue(p.Key, out key))
{
key = Obj.DeepCopy(p.Key, dict);
}
if (p.Value != null)
{
if (!dict.TryGetValue(p.Value, out value))
{
value = Obj.DeepCopy(p.Value, dict);
}
}
else
{
value = p.Value;
}
dst[key] = value;
}
return dst;
}
#endregion Public Methods
#region Private Methods
/// <summary>
/// Helper that make deep copy of array
/// </summary>
/// <typeparam name="T">Type of Array items</typeparam>
/// <param name="src">Source array</param>
/// <param name="dic">
/// Dictionary, that accumulate copies of objects, to have one object - one copy
/// </param>
/// <returns>Deep copy of array</returns>
private static T[] m_deepcopy_array<T>(T[] src, Dictionary<object, object> dic)
{
var t = typeof(T);
object o;
T[] dst;
if (dic.TryGetValue(src, out o))
{
return (T[])o;
}
dst = new T[src.Length];
if (t.IsSimple())
{
Array.Copy(src, dst, src.Length);
}
else
{
dic[src] = dst;
for (int i = 0; i < src.Length; ++i)
{
dst[i] = (T)Obj.m_deepcopy(src[i], dic);
}
}
return dst;
}
/// <summary>
/// Get method of deep object copy
/// </summary>
/// <param name="t">Source object type</param>
/// <returns>Method info if present, else null</returns>
private static MethodInfo m_get_info(Type t)
{
if (t.IsArray && t.GetArrayRank() == 1)
{
var arrt = t.GetElementType();
return typeof(CopyBaseHelper).GetMethod(nameof(CopyBaseHelper.m_deepcopy_array), FInternalStatic).MakeGenericMethod(arrt);
}
if (t == typeof(Hashtable))
{
return typeof(CopyBaseHelper).GetMethod(nameof(CopyBaseHelper.m_deep_copy_hashtable), FPublicStatic);
}
if (t.IsGenericType)
{
var generic = t.GetGenericTypeDefinition();
if (generic == typeof(Dictionary<,>))
{
Type keyType = t.GetGenericArguments()[0];
Type valueType = t.GetGenericArguments()[1];
return typeof(CopyBaseHelper).GetMethod(nameof(CopyBaseHelper.m_deep_copy_dict), FPublicStatic).MakeGenericMethod(keyType, valueType);
}
else if (generic == typeof(HashSet<>))
{
Type valueType = t.GetGenericArguments()[0];
return typeof(CopyBaseHelper).GetMethod(nameof(CopyBaseHelper.m_deep_copy_hashset), FPublicStatic).MakeGenericMethod(valueType);
}
}
return null;
}
#endregion Private Methods
}
}