-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectionUtils.cs
185 lines (149 loc) · 4.97 KB
/
ReflectionUtils.cs
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
#undef NETFX_CORE
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Binocle
{
/// <summary>
/// helper class to fetch property delegates
/// </summary>
class ReflectionUtils
{
public static Assembly getAssembly(Type type)
{
#if NETFX_CORE
return type.GetTypeInfo().Assembly;
#else
return type.Assembly;
#endif
}
public static FieldInfo getFieldInfo(System.Object targetObject, string fieldName)
{
FieldInfo fieldInfo = null;
var type = targetObject.GetType();
#if NETFX_CORE
foreach( var fi in type.GetRuntimeFields() )
{
if( fi.Name == fieldName )
{
fieldInfo = fi;
break;
}
}
#else
do
{
fieldInfo = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
type = type.BaseType;
} while (fieldInfo == null && type != null);
#endif
return fieldInfo;
}
public static IEnumerable<FieldInfo> getFields(Type type)
{
#if NETFX_CORE
return type.GetRuntimeFields();
#else
return type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
#endif
}
public static object getFieldValue(object targetObject, string fieldName)
{
var fieldInfo = getFieldInfo(targetObject, fieldName);
return fieldInfo.GetValue(targetObject);
}
public static PropertyInfo getPropertyInfo(System.Object targetObject, string propertyName)
{
#if NETFX_CORE
return targetObject.GetType().GetRuntimeProperty( propertyName );
#else
return targetObject.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
#endif
}
public static IEnumerable<PropertyInfo> getProperties(Type type)
{
#if NETFX_CORE
return type.GetRuntimeProperties();
#else
return type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
#endif
}
public static MethodInfo getPropertyGetter(PropertyInfo prop)
{
#if NETFX_CORE
return prop.GetMethod;
#else
return prop.GetGetMethod(true);
#endif
}
public static MethodInfo getPropertySetter(PropertyInfo prop)
{
#if NETFX_CORE
return prop.SetMethod;
#else
return prop.GetSetMethod(true);
#endif
}
public static object getPropertyValue(object targetObject, string propertyName)
{
var propInfo = getPropertyInfo(targetObject, propertyName);
var methodInfo = getPropertyGetter(propInfo);
return methodInfo.Invoke(targetObject, new object[] { });
}
public static IEnumerable<MethodInfo> getMethods(Type type)
{
#if NETFX_CORE
return type.GetRuntimeMethods();
#else
return type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
#endif
}
public static MethodInfo getMethodInfo(System.Object targetObject, string methodName)
{
return getMethodInfo(targetObject.GetType(), methodName);
}
public static MethodInfo getMethodInfo(Type type, string methodName)
{
#if NETFX_CORE
foreach( var method in type.GetRuntimeMethods() )
if( method.Name == methodName )
return method;
return null;
#else
return type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
#endif
}
public static T createDelegate<T>(System.Object targetObject, MethodInfo methodInfo)
{
#if NETFX_CORE
return (T)(object)methodInfo.CreateDelegate( typeof( T ), targetObject );
#else
return (T)(object)Delegate.CreateDelegate(typeof(T), targetObject, methodInfo);
#endif
}
/// <summary>
/// either returns a super fast Delegate to set the given property or null if it couldn't be found
/// via reflection
/// </summary>
public static T setterForProperty<T>(System.Object targetObject, string propertyName)
{
// first get the property
var propInfo = getPropertyInfo(targetObject, propertyName);
if (propInfo == null)
return default(T);
return createDelegate<T>(targetObject, propInfo.GetSetMethod());
}
/// <summary>
/// either returns a super fast Delegate to get the given property or null if it couldn't be found
/// via reflection
/// </summary>
public static T getterForProperty<T>(System.Object targetObject, string propertyName)
{
// first get the property
var propInfo = getPropertyInfo(targetObject, propertyName);
if (propInfo == null)
return default(T);
return createDelegate<T>(targetObject, propInfo.GetSetMethod());
}
}
}