1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.TestSupport
18 using System
.Reflection
;
20 public static class ReflectionHelper
22 public static object RunInstanceMethod
<ObjectType
>(ObjectType objectInstance
, string methodName
)
24 return RunInstanceMethod(typeof(ObjectType
), objectInstance
, methodName
);
27 public static object RunInstanceMethod(Type objectType
, object objectInstance
, string methodName
)
29 object[] methodParameters
= new object[0];
30 return RunInstanceMethod(objectType
, objectInstance
, methodName
, ref methodParameters
, BindingFlags
.Default
);
33 public static object RunInstanceMethod
<ObjectType
>(ObjectType objectInstance
, string methodName
, ref object[] methodParameters
)
35 return RunInstanceMethod(typeof(ObjectType
), objectInstance
, methodName
, ref methodParameters
);
38 public static object RunInstanceMethod(Type objectType
, object objectInstance
, string methodName
, ref object[] methodParameters
)
40 return RunInstanceMethod(objectType
, objectInstance
, methodName
, ref methodParameters
, BindingFlags
.Default
);
43 public static object RunInstanceMethod
<ObjectType
>(ObjectType objectInstance
, string methodName
, ref object[] methodParameters
, BindingFlags extraFlags
)
45 return RunInstanceMethod(typeof(ObjectType
), objectInstance
, methodName
, ref methodParameters
, extraFlags
);
48 public static object RunInstanceMethod(Type objectType
, object objectInstance
, string methodName
, ref object[] methodParameters
, BindingFlags extraFlags
)
50 BindingFlags eFlags
= BindingFlags
.Instance
| BindingFlags
.Public
| BindingFlags
.NonPublic
| extraFlags
;
51 return RunMethod(objectType
, objectInstance
, methodName
, ref methodParameters
, eFlags
);
54 private static object RunMethod(Type objectType
, object objectInstance
, string methodName
, ref object[] methodParameters
, BindingFlags bindingFlags
)
58 MethodInfo methodInfo
= objectType
.GetMethod(methodName
, bindingFlags
);
60 if (methodInfo
== null)
61 throw new ArgumentException("There is no method '" + methodName
+ "' for type '" + objectType
+ "'.");
63 object returnValue
= methodInfo
.Invoke(objectInstance
, methodParameters
);
67 catch (TargetInvocationException ex
)
69 throw (ex
.InnerException
!= null) ? ex
.InnerException
: ex
;
73 public static object RunStaticMethod(Type objectType
, string methodName
)
75 object[] methodParameters
= new object[0];
76 return RunStaticMethod(objectType
, methodName
, ref methodParameters
, BindingFlags
.Default
);
79 public static object RunStaticMethod(Type objectType
, string methodName
, ref object[] methodParameters
)
81 return RunStaticMethod(objectType
, methodName
, ref methodParameters
, BindingFlags
.Default
);
84 public static object RunStaticMethod(Type objectType
, string methodName
, ref object[] methodParameters
, BindingFlags extraFlags
)
86 BindingFlags bindingFlags
= BindingFlags
.Static
| BindingFlags
.Public
| BindingFlags
.NonPublic
| extraFlags
;
87 return RunMethod(objectType
, null, methodName
, ref methodParameters
, bindingFlags
);
90 public static ReturnType GetField
<ReturnType
, ObjectType
>(ObjectType objectInstance
, string fieldName
)
92 FieldInfo fieldInfo
= typeof(ObjectType
).GetField(fieldName
, BindingFlags
.Instance
| BindingFlags
.Public
| BindingFlags
.NonPublic
);
93 return (ReturnType
)fieldInfo
.GetValue(objectInstance
);
96 public static void SetField
<ObjectType
>(ObjectType objectInstance
, string fieldName
, object fieldValue
)
98 SetField(typeof(ObjectType
), objectInstance
, fieldName
, fieldValue
);
101 public static void SetField(Type objectType
, object objectInstance
, string fieldName
, object fieldValue
)
103 FieldInfo fieldInfo
= objectType
.GetField(fieldName
, BindingFlags
.Instance
| BindingFlags
.Public
| BindingFlags
.NonPublic
);
104 fieldInfo
.SetValue(objectInstance
, fieldValue
);