forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeExtensions.cs
More file actions
128 lines (108 loc) · 4.26 KB
/
TypeExtensions.cs
File metadata and controls
128 lines (108 loc) · 4.26 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
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
namespace ServiceStack
{
public delegate object ObjectActivator(params object[] args);
public static class TypeExtensions
{
public static Type[] GetReferencedTypes(this Type type)
{
var refTypes = new HashSet<Type> { type };
AddReferencedTypes(type, refTypes);
return refTypes.ToArray();
}
public static void AddReferencedTypes(Type type, HashSet<Type> refTypes)
{
if (type.BaseType() != null)
{
if (!refTypes.Contains(type.BaseType()))
{
refTypes.Add(type.BaseType());
AddReferencedTypes(type.BaseType(), refTypes);
}
if (!type.BaseType().GetGenericArguments().IsEmpty())
{
foreach (var arg in type.BaseType().GetGenericArguments())
{
if (!refTypes.Contains(arg))
{
refTypes.Add(arg);
AddReferencedTypes(arg, refTypes);
}
}
}
}
var properties = type.GetPropertyInfos();
if (!properties.IsEmpty())
{
foreach (var p in properties)
{
if (!refTypes.Contains(p.PropertyType))
{
refTypes.Add(p.PropertyType);
AddReferencedTypes(type, refTypes);
}
var args = p.PropertyType.GetGenericArguments();
if (!args.IsEmpty())
{
foreach (var arg in args)
{
if (!refTypes.Contains(arg))
{
refTypes.Add(arg);
AddReferencedTypes(arg, refTypes);
}
}
}
else if (p.PropertyType.IsArray)
{
var elType = p.PropertyType.GetElementType();
if (!refTypes.Contains(elType))
{
refTypes.Add(elType);
AddReferencedTypes(elType, refTypes);
}
}
}
}
}
public static ObjectActivator GetActivatorToCache(ConstructorInfo ctor)
{
var pi = ctor.GetParameters();
var paramArgs = Expression.Parameter(typeof(object[]), "args");
var exprArgs = new Expression[pi.Length];
for (int i = 0; i < pi.Length; i++)
{
var index = Expression.Constant(i);
var paramType = pi[i].ParameterType;
var paramAccessorExp = Expression.ArrayIndex(paramArgs, index);
var paramCastExp = Expression.Convert(paramAccessorExp, paramType);
exprArgs[i] = paramCastExp;
}
var newExp = Expression.New(ctor, exprArgs);
var lambda = Expression.Lambda(typeof(ObjectActivator), newExp, paramArgs);
var ctorFn = (ObjectActivator)lambda.Compile();
return ctorFn;
}
static Dictionary<ConstructorInfo, ObjectActivator> ActivatorCache =
new Dictionary<ConstructorInfo, ObjectActivator>();
public static ObjectActivator GetActivator(this ConstructorInfo ctor)
{
ObjectActivator fn;
if (ActivatorCache.TryGetValue(ctor, out fn))
return fn;
fn = GetActivatorToCache(ctor);
Dictionary<ConstructorInfo, ObjectActivator> snapshot, newCache;
do
{
snapshot = ActivatorCache;
newCache = new Dictionary<ConstructorInfo, ObjectActivator>(ActivatorCache) { [ctor] = fn };
} while (!ReferenceEquals(
Interlocked.CompareExchange(ref ActivatorCache, newCache, snapshot), snapshot));
return fn;
}
}
}