forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionUtils.cs
More file actions
423 lines (353 loc) · 15.6 KB
/
Copy pathReflectionUtils.cs
File metadata and controls
423 lines (353 loc) · 15.6 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using ServiceStack.Common.Support;
using ServiceStack.Logging;
using ServiceStack.Net30.Collections.Concurrent;
namespace ServiceStack.Common.Utils
{
public class ReflectionUtils
{
public static readonly ILog Log = LogManager.GetLogger(typeof(ReflectionUtils));
/// <summary>
/// Populate an object with Example data.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static object PopulateObject(object obj)
{
if (obj == null) return null;
return PopulateObjectInternal(obj, new Dictionary<Type, int>(20));
}
/// <summary>
/// Populates the object with example data.
/// </summary>
/// <param name="obj"></param>
/// <param name="recursionInfo">Tracks how deeply nested we are</param>
/// <returns></returns>
private static object PopulateObjectInternal(object obj, Dictionary<Type,int> recursionInfo)
{
if (obj == null) return null;
if (obj is string) return obj; // prevents it from dropping into the char[] Chars property. Sheesh
var members = obj.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance);
foreach (var info in members)
{
var fieldInfo = info as FieldInfo;
var propertyInfo = info as PropertyInfo;
if (fieldInfo != null || propertyInfo != null)
{
var memberType = fieldInfo != null ? fieldInfo.FieldType : propertyInfo.PropertyType;
var value = CreateDefaultValue(memberType, recursionInfo);
SetValue(fieldInfo, propertyInfo, obj, value);
}
}
return obj;
}
private static readonly Dictionary<Type, object> DefaultValueTypes
= new Dictionary<Type, object>();
public static object GetDefaultValue(Type type)
{
if (!type.IsValueType) return null;
object defaultValue;
lock (DefaultValueTypes)
{
if (!DefaultValueTypes.TryGetValue(type, out defaultValue))
{
defaultValue = Activator.CreateInstance(type);
DefaultValueTypes[type] = defaultValue;
}
}
return defaultValue;
}
private static readonly ConcurrentDictionary<string, AssignmentDefinition> AssignmentDefinitionCache
= new ConcurrentDictionary<string, AssignmentDefinition>();
public static AssignmentDefinition GetAssignmentDefinition(Type toType, Type fromType)
{
var cacheKey = toType.FullName + "<" + fromType.FullName;
return AssignmentDefinitionCache.GetOrAdd(cacheKey, delegate {
var definition = new AssignmentDefinition {
ToType = toType,
FromType = fromType,
};
var members = fromType.GetMembers(BindingFlags.Public | BindingFlags.Instance);
foreach (var info in members)
{
var fromPropertyInfo = info as PropertyInfo;
if (fromPropertyInfo != null)
{
var toPropertyInfo = GetPropertyInfo(toType, fromPropertyInfo.Name);
if (toPropertyInfo == null) continue;
if (!fromPropertyInfo.CanRead) continue;
if (!toPropertyInfo.CanWrite) continue;
definition.AddMatch(fromPropertyInfo, toPropertyInfo);
}
var fromFieldInfo = info as FieldInfo;
if (fromFieldInfo != null)
{
var toFieldInfo = GetFieldInfo(toType, fromFieldInfo.Name);
if (toFieldInfo == null) continue;
definition.AddMatch(fromFieldInfo, toFieldInfo);
}
}
return definition;
});
}
public static To PopulateObject<To, From>(To to, From from)
{
if (Equals(to, default(To)) || Equals(from, default(From))) return default(To);
var assignmentDefinition = GetAssignmentDefinition(to.GetType(), from.GetType());
assignmentDefinition.Populate(to, from);
return to;
}
public static To PopulateWithNonDefaultValues<To, From>(To to, From from)
{
if (Equals(to, default(To)) || Equals(from, default(From))) return default(To);
var assignmentDefinition = GetAssignmentDefinition(to.GetType(), from.GetType());
assignmentDefinition.PopulateWithNonDefaultValues(to, from);
return to;
}
public static To PopulateFromPropertiesWithAttribute<To, From>(To to, From from,
Type attributeType)
{
if (Equals(to, default(To)) || Equals(from, default(From))) return default(To);
var assignmentDefinition = GetAssignmentDefinition(to.GetType(), from.GetType());
assignmentDefinition.PopulateFromPropertiesWithAttribute(to, from, attributeType);
return to;
}
public static void SetProperty(object obj, PropertyInfo propertyInfo, object value)
{
if (!propertyInfo.CanWrite)
{
Log.WarnFormat("Attempted to set read only property '{0}'", propertyInfo.Name);
return;
}
var propertySetMetodInfo = propertyInfo.GetSetMethod();
if (propertySetMetodInfo != null)
{
propertySetMetodInfo.Invoke(obj, new[] { value });
}
}
public static object GetProperty(object obj, PropertyInfo propertyInfo)
{
if (propertyInfo == null || !propertyInfo.CanRead)
return null;
var getMethod = propertyInfo.GetGetMethod();
return getMethod != null ? getMethod.Invoke(obj, new object[0]) : null;
}
public static void SetValue(FieldInfo fieldInfo, PropertyInfo propertyInfo, object obj, object value)
{
try
{
if (IsUnsettableValue(fieldInfo, propertyInfo)) return;
if (fieldInfo != null && !fieldInfo.IsLiteral)
{
fieldInfo.SetValue(obj, value);
}
else
{
SetProperty(obj, propertyInfo, value);
}
}
catch (Exception ex)
{
var name = (fieldInfo != null) ? fieldInfo.Name : propertyInfo.Name;
Log.DebugFormat("Could not set member: {0}. Error: {1}", name, ex.Message);
}
}
public static bool IsUnsettableValue(FieldInfo fieldInfo, PropertyInfo propertyInfo)
{
if (propertyInfo != null && propertyInfo.ReflectedType != null)
{
// Properties on non-user defined classes should not be set
// Currently we define those properties as properties declared on
// types defined in mscorlib
if (propertyInfo.DeclaringType.Assembly == typeof(object).Assembly)
{
return true;
}
}
return false;
}
public static object[] CreateDefaultValues(IEnumerable<Type> types, Dictionary<Type, int> recursionInfo)
{
var values = new List<object>();
foreach (var type in types)
{
values.Add(CreateDefaultValue(type, recursionInfo));
}
return values.ToArray();
}
private const int MaxRecursionLevelForDefaultValues = 2; // do not nest a single type more than this deep.
public static object CreateDefaultValue(Type type, Dictionary<Type, int> recursionInfo)
{
if (type == typeof(string))
{
return type.Name;
}
if (type.IsEnum)
{
#if SILVERLIGHT4
return Enum.ToObject(type, 0);
#else
return Enum.GetValues(type).GetValue(0);
#endif
}
// If we have hit our recursion limit for this type, then return null
int recurseLevel; // will get set to 0 if TryGetValue() fails
recursionInfo.TryGetValue(type, out recurseLevel);
if (recurseLevel > MaxRecursionLevelForDefaultValues) return null;
recursionInfo[type] = recurseLevel + 1; // increase recursion level for this type
try // use a try/finally block to make sure we decrease the recursion level for this type no matter which code path we take,
{
//when using KeyValuePair<TKey, TValue>, TKey must be non-default to stuff in a Dictionary
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
{
var genericTypes = type.GetGenericArguments();
var valueType = Activator.CreateInstance(type, CreateDefaultValue(genericTypes[0], recursionInfo), CreateDefaultValue(genericTypes[1], recursionInfo));
return PopulateObjectInternal(valueType, recursionInfo);
}
if (type.IsValueType)
{
return Activator.CreateInstance(type);
}
if (type.IsArray)
{
return PopulateArray(type, recursionInfo);
}
var constructorInfo = type.GetConstructor(Type.EmptyTypes);
var hasEmptyConstructor = constructorInfo != null;
if (hasEmptyConstructor)
{
var value = constructorInfo.Invoke(new object[0]);
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
Type[] interfaces = type.FindInterfaces((t, critera) =>
t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICollection<>)
, null);
bool isGenericCollection = interfaces.Length > 0;
if (isGenericCollection)
{
SetGenericCollection(interfaces[0], value, recursionInfo);
}
#endif
//when the object might have nested properties such as enums with non-0 values, etc
return PopulateObjectInternal(value, recursionInfo);
}
return null;
}
finally
{
recursionInfo[type] = recurseLevel;
}
}
public static void SetGenericCollection(Type realisedListType, object genericObj, Dictionary<Type, int> recursionInfo)
{
var args = realisedListType.GetGenericArguments();
if (args.Length != 1)
{
Log.ErrorFormat("Found a generic list that does not take one generic argument: {0}", realisedListType);
return;
}
var methodInfo = realisedListType.GetMethod("Add");
if (methodInfo != null)
{
var argValues = CreateDefaultValues(args, recursionInfo);
methodInfo.Invoke(genericObj, argValues);
}
}
public static Array PopulateArray(Type type, Dictionary<Type, int> recursionInfo)
{
var elementType = type.GetElementType();
var objArray = Array.CreateInstance(elementType, 1);
var objElementType = CreateDefaultValue(elementType, recursionInfo);
objArray.SetValue(objElementType, 0);
return objArray;
}
//TODO: replace with InAssignableFrom
public static bool CanCast(Type toType, Type fromType)
{
if (toType.IsInterface)
{
var interfaceList = fromType.GetInterfaces().ToList();
if (interfaceList.Contains(toType)) return true;
}
else
{
Type baseType = fromType;
bool areSameTypes;
do
{
areSameTypes = baseType == toType;
}
while (!areSameTypes && (baseType = fromType.BaseType) != null);
if (areSameTypes) return true;
}
return false;
}
public static MemberInfo GetMemberInfo(Type fromType, string memberName)
{
var baseType = fromType;
do
{
var members = baseType.GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (var memberInfo in members)
{
if (memberInfo.Name == memberName) return memberInfo;
}
}
while ((baseType = baseType.BaseType) != null);
return null;
}
public static FieldInfo GetFieldInfo(Type fromType, string fieldName)
{
var baseType = fromType;
do
{
var fieldInfos = baseType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (var fieldInfo in fieldInfos)
{
if (fieldInfo.Name == fieldName) return fieldInfo;
}
}
while ((baseType = baseType.BaseType) != null);
return null;
}
public static PropertyInfo GetPropertyInfo(Type fromType, string propertyName)
{
var baseType = fromType;
do
{
var propertyInfos = baseType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.Name == propertyName) return propertyInfo;
}
}
while ((baseType = baseType.BaseType) != null);
return null;
}
public static IEnumerable<KeyValuePair<PropertyInfo, T>> GetPropertyAttributes<T>(Type fromType) where T : Attribute
{
var attributeType = typeof(T);
var baseType = fromType;
do
{
var propertyInfos = baseType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (var propertyInfo in propertyInfos)
{
var attributes = propertyInfo.GetCustomAttributes(attributeType, true);
foreach (T attribute in attributes)
{
yield return new KeyValuePair<PropertyInfo, T>(propertyInfo, attribute);
}
}
}
while ((baseType = baseType.BaseType) != null);
}
public static object CreateInstance(Type type)
{
return Text.ReflectionExtensions.CreateInstance(type);
}
}
}