This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Reflection.cs
311 lines (259 loc) · 10.9 KB
/
Reflection.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RSG.Utils
{
/// <summary>
/// Provider of reflection services.
/// </summary>
public interface IReflection
{
/// <summary>
/// Returns true when the type has a property with the specified name.
/// </summary>
bool HasProperty(Type type, string propertyName);
/// <summary>
/// Get the value of a property.
/// </summary>
T GetPropertyValue<T>(object obj, string propertyName);
/// <summary>
/// Sets the value of the property.
/// </summary>
void SetPropertyValue<T>(object obj, string propertyName, T propertyValue);
/// <summary>
/// Gets all of the properties for this object
/// </summary>
string[] GetPropertyNames(object obj);
/// <summary>
/// Returns true if the requested property name contains the specified attribute
/// </summary>
bool PropertyHasAttribute(object obj, string propertyName, Type attributeType);
Type GetPropertyType(Type type, string propertyName);
T GetPropertyAttribute<T>(object obj, string propertyName)
where T : Attribute;
/// <summary>
/// Retrieves the class attribute object of the type specified
/// returns null if attribute not found
/// </summary>
AttributeT GetClassAttribute<AttributeT>(Type type)
where AttributeT : Attribute;
/// <summary>
/// Gets the type of the supplied object
/// </summary>
Type GetObjectType(object target);
/// <summary>
/// Get the names of types that impemented the specified interface.
/// </summary>
IEnumerable<Type> GetTypesThatImplement<T>();
/// <summary>
/// Get the names of types that impemented the specified interface.
/// </summary>
IEnumerable<string> GetTypeNamesThatImplement<T>();
/// <summary>
/// Invoke a method with the name [methodName] on [parentObject] with the arguments [args]
/// argumentValueConverter converts/marshals arguments to the expected type.
/// </summary>
object InvokeMethod(object parentObject, string methodName, object[] args);
/// <summary>
/// Invoke a method with the name [methodName] on [parentObject] with the arguments [args]
/// argumentValueConverter converts/marshals arguments to the expected type.
/// </summary>
object InvokeMethod(object parentObject, string methodName, object[] args, Func<object, Type, object> argumentValueConverter);
/// <summary>
/// Find all types marked by the specified attributes.
/// </summary>
IEnumerable<Type> FindTypesMarkedByAttributes(IEnumerable<Type> attributesTypes);
/// <summary>
/// Get all attributes attached to the specified type.
/// </summary>
IEnumerable<AttributeT> GetAttributes<AttributeT>(Type type)
where AttributeT : Attribute;
/// <summary>
/// Returns true if the requested types has the specified attribute.
/// </summary>
bool HasAttribute<AttributeT>(Type type)
where AttributeT : Attribute;
}
/// <summary>
/// Provider of reflection services.
/// </summary>
public class Reflection : IReflection
{
/// <summary>
/// Used to filter assemblies when searching for types, etc.
/// </summary>
private Predicate<string> assemblyIgnoreFilter;
public Reflection()
{
}
public Reflection(Predicate<string> assemblyIgnoreFilter)
{
Argument.NotNull(() => assemblyIgnoreFilter);
this.assemblyIgnoreFilter = assemblyIgnoreFilter;
}
/// <summary>
/// Returns true when the type has a property with the specified name.
/// </summary>
public bool HasProperty(Type type, string propertyName)
{
return ReflectionUtils.HasProperty(type, propertyName);
}
/// <summary>
/// Get the value of a property.
/// </summary>
public T GetPropertyValue<T>(object obj, string propertyName)
{
return (T)ReflectionUtils.GetPropertyValue(obj, propertyName);
}
/// <summary>
/// Sets the value of the property.
/// </summary>
public void SetPropertyValue<T>(object obj, string propertyName, T propertyValue)
{
ReflectionUtils.SetPropertyValue(obj, propertyName, propertyValue);
}
/// <summary>
/// Gets all of the properties for this object
/// </summary>
public string[] GetPropertyNames(object obj)
{
Type type = obj.GetType();
string[] properties = type.GetProperties().Select(p => p.Name).ToArray();
return properties;
}
/// <summary>
/// Returns true if the requested property name contains the specified attribute
/// </summary>
public bool PropertyHasAttribute(object obj, string propertyName, Type attributeType)
{
var property = obj.GetType().GetProperty(propertyName);
if (property == null)
{
throw new ApplicationException("Failed to get property, Property " + propertyName + " not found for object " + obj.GetType().Name);
}
return ReflectionUtils.PropertyHasAttribute(property, attributeType);
}
public Type GetPropertyType(Type type, string propertyName)
{
return ReflectionUtils.GetPropertyType(type, propertyName);
}
public T GetPropertyAttribute<T>(object obj, string propertyName)
where T : Attribute
{
var property = obj.GetType().GetProperty(propertyName);
if (property == null)
{
throw new ApplicationException("Failed to get property, Property " + propertyName + " not found for object " + obj.GetType().Name);
}
return ReflectionUtils.GetPropertyAttribute<T>(property);
}
/// <summary>
/// Retrieves the class attribute object of the type specified
/// returns null if attribute not found
/// </summary>
public AttributeT GetClassAttribute<AttributeT>(Type type)
where AttributeT : Attribute
{
return ReflectionUtils.GetAttribute<AttributeT>(type);
}
/// <summary>
/// Gets the type of the supplied object
/// </summary>
public Type GetObjectType(object target)
{
return target.GetType();
}
/// <summary>
/// Get the names of types that impemented the specified interface.
/// </summary>
public IEnumerable<Type> GetTypesThatImplement<T>()
{
return ReflectionUtils
.GetAllTypes()
.Where(t => ReflectionUtils.ClassImplements(t, typeof(T)))
.Where(t => !t.IsInterface);
}
/// <summary>
/// Get the names of types that impemented the specified interface.
/// </summary>
public IEnumerable<string> GetTypeNamesThatImplement<T>()
{
return GetTypesThatImplement<T>()
.Select(t => t.Name);
}
/// <summary>
/// Invoke a method with the name [methodName] on [parentObject] with the arguments [args]
/// argumentValueConverter converts/marshals arguments to the expected type.
/// </summary>
public object InvokeMethod(object parentObject, string methodName, object[] args)
{
return InvokeMethod(parentObject, methodName, args, (a, t) => a);
}
/// <summary>
/// Invoke a method with the name [methodName] on [parentObject] with the arguments [args]
/// argumentValueConverter converts/marshals arguments to the expected type.
/// </summary>
public object InvokeMethod(object parentObject, string methodName, object[] args, Func<object, Type, object> argumentValueConverter)
{
Argument.NotNull(() => parentObject);
Argument.StringNotNullOrEmpty(() => methodName);
Argument.NotNull(() => args);
Argument.NotNull(() => argumentValueConverter);
try
{
int numArgs = args.Count();
var method = ReflectionUtils.GetMethod(parentObject.GetType(), methodName, numArgs);
if(method == null)
{
throw new ApplicationException("Failed to find method " + methodName + " with " + numArgs + " arguments on type " + parentObject.GetType().Name);
}
var methodParameters = method.GetParameters();
if(numArgs == 0 && methodParameters.Count() > 0)
{
throw new ApplicationException("Got no arguments, expected " + methodParameters.Count());
}
else if(numArgs != methodParameters.Count())
{
throw new ApplicationException("Got " + numArgs + " arguments, expected " + methodParameters.Count());
}
var convertedArgs = new List<object>();
for (int argIndex = 0; argIndex < methodParameters.Count(); ++argIndex)
{
convertedArgs.Add(argumentValueConverter(args[argIndex], methodParameters[argIndex].ParameterType));
}
return ReflectionUtils.InvokeMethod(parentObject, method, convertedArgs.ToArray());
}
catch (Exception e)
{
throw new ApplicationException("Invocation of method " + methodName + " failed, " + e);
}
}
/// <summary>
/// Find all types marked by the specified attributes.
/// </summary>
public IEnumerable<Type> FindTypesMarkedByAttributes(IEnumerable<Type> attributesTypes)
{
Argument.NotNull(() => attributesTypes);
return ReflectionUtils.FindTypesMarkedByAttributes(attributesTypes);
}
/// <summary>
/// Get all attributes attached to the specified type.
/// </summary>
public IEnumerable<AttributeT> GetAttributes<AttributeT>(Type type)
where AttributeT : Attribute
{
Argument.NotNull(() => type);
return ReflectionUtils.GetAttributes<AttributeT>(type);
}
/// <summary>
/// Returns true if the requested types has the specified attribute.
/// </summary>
public bool HasAttribute<AttributeT>(Type type)
where AttributeT : Attribute
{
Argument.NotNull(() => type);
return GetAttributes<AttributeT>(type).Any();
}
}
}