forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionExtensions.cs
More file actions
196 lines (174 loc) · 6.93 KB
/
ReflectionExtensions.cs
File metadata and controls
196 lines (174 loc) · 6.93 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
using System;
using System.Reflection;
using ServiceStack.Common.Utils;
namespace ServiceStack.Common
{
public static class ReflectionExtensions
{
public static To PopulateWith<To, From>(this To to, From from)
{
return ReflectionUtils.PopulateObject(to, from);
}
public static To PopulateWithNonDefaultValues<To, From>(this To to, From from)
{
return ReflectionUtils.PopulateWithNonDefaultValues(to, from);
}
public static To PopulateFromPropertiesWithAttribute<To, From, TAttr>(this To to, From from)
{
return ReflectionUtils.PopulateFromPropertiesWithAttribute(to, from, typeof(TAttr));
}
public static T TranslateTo<T>(this object from)
where T : new()
{
var to = new T();
return to.PopulateWith(from);
}
public static TAttribute FirstAttribute<TAttribute>(this Type type)
{
return type.FirstAttribute<TAttribute>(true);
}
public static TAttribute FirstAttribute<TAttribute>(this Type type, bool inherit)
{
var attrs = type.GetCustomAttributes(typeof(TAttribute), inherit);
return (TAttribute)(attrs.Length > 0 ? attrs[0] : null);
}
public static TAttribute FirstAttribute<TAttribute>(this PropertyInfo propertyInfo)
{
return propertyInfo.FirstAttribute<TAttribute>(true);
}
public static TAttribute FirstAttribute<TAttribute>(this PropertyInfo propertyInfo, bool inherit)
{
var attrs = propertyInfo.GetCustomAttributes(typeof(TAttribute), inherit);
return (TAttribute)(attrs.Length > 0 ? attrs[0] : null);
}
public static bool IsGenericType(this Type type)
{
while (type != null)
{
if (type.IsGenericType)
return true;
type = type.BaseType;
}
return false;
}
public static Type FirstGenericTypeDefinition(this Type type)
{
while (type != null)
{
if (type.IsGenericType)
return type.GetGenericTypeDefinition();
type = type.BaseType;
}
return null;
}
public static bool IsDynamic(this Assembly assembly)
{
#if MONOTOUCH
return false;
#else
try
{
var isDyanmic = assembly is System.Reflection.Emit.AssemblyBuilder
|| string.IsNullOrEmpty(assembly.Location);
return isDyanmic;
}
catch (NotSupportedException)
{
//Ignore assembly.Location not supported in a dynamic assembly.
return true;
}
#endif
}
}
}
#if FALSE && DOTNET35
//Efficient POCO Translator from: http://www.yoda.arachsys.com/csharp/miscutil/
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace MiscUtil.Reflection
{
/// <summary>
/// Generic class which copies to its target type from a source
/// type specified in the Copy method. The types are specified
/// separately to take advantage of type inference on generic
/// method arguments.
/// </summary>
public static class PropertyCopy<TTarget> where TTarget : class, new()
{
/// <summary>
/// Copies all readable properties from the source to a new instance
/// of TTarget.
/// </summary>
public static TTarget CopyFrom<TSource>(TSource source) where TSource : class
{
return PropertyCopier<TSource>.Copy(source);
}
/// <summary>
/// Static class to efficiently store the compiled delegate which can
/// do the copying. We need a bit of work to ensure that exceptions are
/// appropriately propagated, as the exception is generated at type initialization
/// time, but we wish it to be thrown as an ArgumentException.
/// </summary>
private static class PropertyCopier<TSource> where TSource : class
{
private static readonly Func<TSource, TTarget> copier;
private static readonly Exception initializationException;
internal static TTarget Copy(TSource source)
{
if (initializationException != null)
{
throw initializationException;
}
if (source == null)
{
throw new ArgumentNullException("source");
}
return copier(source);
}
static PropertyCopier()
{
try
{
copier = BuildCopier();
initializationException = null;
}
catch (Exception e)
{
copier = null;
initializationException = e;
}
}
private static Func<TSource, TTarget> BuildCopier()
{
ParameterExpression sourceParameter = Expression.Parameter(typeof(TSource), "source");
var bindings = new List<MemberBinding>();
foreach (PropertyInfo sourceProperty in typeof(TSource).GetProperties())
{
if (!sourceProperty.CanRead)
{
continue;
}
PropertyInfo targetProperty = typeof(TTarget).GetProperty(sourceProperty.Name);
if (targetProperty == null)
{
throw new ArgumentException("Property " + sourceProperty.Name + " is not present and accessible in " + typeof(TTarget).FullName);
}
if (!targetProperty.CanWrite)
{
throw new ArgumentException("Property " + sourceProperty.Name + " is not writable in " + typeof(TTarget).FullName);
}
if (!targetProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType))
{
throw new ArgumentException("Property " + sourceProperty.Name + " has an incompatible type in " + typeof(TTarget).FullName);
}
bindings.Add(Expression.Bind(targetProperty, Expression.Property(sourceParameter, sourceProperty)));
}
Expression initializer = Expression.MemberInit(Expression.New(typeof(TTarget)), bindings);
return Expression.Lambda<Func<TSource,TTarget>>(initializer, sourceParameter).Compile();
}
}
}
}
#endif