@@ -29,7 +29,7 @@ public static MethodInfo GetMethodDefinition<TSource>(Expression<Action<TSource>
29
29
public static MethodInfo GetMethod < TSource > ( Expression < Action < TSource > > method )
30
30
{
31
31
if ( method == null )
32
- throw new ArgumentNullException ( " method" ) ;
32
+ throw new ArgumentNullException ( nameof ( method ) ) ;
33
33
34
34
return ( ( MethodCallExpression ) method . Body ) . Method ;
35
35
}
@@ -54,11 +54,40 @@ public static MethodInfo GetMethodDefinition(Expression<System.Action> method)
54
54
public static MethodInfo GetMethod ( Expression < System . Action > method )
55
55
{
56
56
if ( method == null )
57
- throw new ArgumentNullException ( " method" ) ;
57
+ throw new ArgumentNullException ( nameof ( method ) ) ;
58
58
59
59
return ( ( MethodCallExpression ) method . Body ) . Method ;
60
60
}
61
61
62
+ /// <summary>
63
+ /// Get the <see cref="MethodInfo"/> for a public overload of a given method if the method does not match
64
+ /// given parameter types, otherwise directly yield the given method.
65
+ /// </summary>
66
+ /// <param name="method">The method for which finding an overload.</param>
67
+ /// <param name="parameterTypes">The arguments types of the overload to get.</param>
68
+ /// <returns>The <see cref="MethodInfo"/> of the method.</returns>
69
+ /// <remarks>Whenever possible, use GetMethod() instead for performance reasons.</remarks>
70
+ public static MethodInfo GetMethodOverload ( MethodInfo method , System . Type [ ] parameterTypes )
71
+ {
72
+ if ( method == null )
73
+ throw new ArgumentNullException ( nameof ( method ) ) ;
74
+ if ( parameterTypes == null )
75
+ throw new ArgumentNullException ( nameof ( parameterTypes ) ) ;
76
+
77
+ if ( ParameterTypesMatch ( method . GetParameters ( ) , parameterTypes ) )
78
+ return method ;
79
+
80
+ var overload = method . DeclaringType . GetMethod ( method . Name ,
81
+ ( method . IsStatic ? BindingFlags . Static : BindingFlags . Instance ) | BindingFlags . Public ,
82
+ null , parameterTypes , null ) ;
83
+
84
+ if ( overload == null )
85
+ throw new InvalidOperationException (
86
+ $ "No overload found for method '{ method . DeclaringType . Name } .{ method . Name } ' and parameter types '{ string . Join ( ", " , parameterTypes . Select ( t => t . Name ) ) } '") ;
87
+
88
+ return overload ;
89
+ }
90
+
62
91
/// <summary>
63
92
/// Gets the field or property to be accessed.
64
93
/// </summary>
@@ -70,7 +99,7 @@ public static MemberInfo GetProperty<TSource, TResult>(Expression<Func<TSource,
70
99
{
71
100
if ( property == null )
72
101
{
73
- throw new ArgumentNullException ( " property" ) ;
102
+ throw new ArgumentNullException ( nameof ( property ) ) ;
74
103
}
75
104
return ( ( MemberExpression ) property . Body ) . Member ;
76
105
}
@@ -91,31 +120,8 @@ internal static System.Type GetPropertyOrFieldType(this MemberInfo memberInfo)
91
120
92
121
return null ;
93
122
}
94
- }
95
-
96
- [ Obsolete ( "Please use ReflectionHelper instead" ) ]
97
- public static class EnumerableHelper
98
- {
99
- public static MethodInfo GetMethod ( string name , System . Type [ ] parameterTypes )
100
- {
101
- return typeof ( Enumerable ) . GetMethods ( BindingFlags . Static | BindingFlags . Public )
102
- . Where ( m => m . Name == name &&
103
- ParameterTypesMatch ( m . GetParameters ( ) , parameterTypes ) )
104
- . Single ( ) ;
105
- }
106
-
107
- public static MethodInfo GetMethod ( string name , System . Type [ ] parameterTypes , System . Type [ ] genericTypeParameters )
108
- {
109
- return typeof ( Enumerable ) . GetMethods ( BindingFlags . Static | BindingFlags . Public )
110
- . Where ( m => m . Name == name &&
111
- m . ContainsGenericParameters &&
112
- m . GetGenericArguments ( ) . Count ( ) == genericTypeParameters . Length &&
113
- ParameterTypesMatch ( m . GetParameters ( ) , parameterTypes ) )
114
- . Single ( )
115
- . MakeGenericMethod ( genericTypeParameters ) ;
116
- }
117
123
118
- private static bool ParameterTypesMatch ( ParameterInfo [ ] parameters , System . Type [ ] types )
124
+ internal static bool ParameterTypesMatch ( ParameterInfo [ ] parameters , System . Type [ ] types )
119
125
{
120
126
if ( parameters . Length != types . Length )
121
127
{
@@ -141,4 +147,27 @@ private static bool ParameterTypesMatch(ParameterInfo[] parameters, System.Type[
141
147
return true ;
142
148
}
143
149
}
150
+
151
+ [ Obsolete ( "Please use ReflectionHelper instead" ) ]
152
+ public static class EnumerableHelper
153
+ {
154
+ public static MethodInfo GetMethod ( string name , System . Type [ ] parameterTypes )
155
+ {
156
+ return typeof ( Enumerable ) . GetMethods ( BindingFlags . Static | BindingFlags . Public )
157
+ . Where ( m => m . Name == name &&
158
+ ReflectionHelper . ParameterTypesMatch ( m . GetParameters ( ) , parameterTypes ) )
159
+ . Single ( ) ;
160
+ }
161
+
162
+ public static MethodInfo GetMethod ( string name , System . Type [ ] parameterTypes , System . Type [ ] genericTypeParameters )
163
+ {
164
+ return typeof ( Enumerable ) . GetMethods ( BindingFlags . Static | BindingFlags . Public )
165
+ . Where ( m => m . Name == name &&
166
+ m . ContainsGenericParameters &&
167
+ m . GetGenericArguments ( ) . Count ( ) == genericTypeParameters . Length &&
168
+ ReflectionHelper . ParameterTypesMatch ( m . GetParameters ( ) , parameterTypes ) )
169
+ . Single ( )
170
+ . MakeGenericMethod ( genericTypeParameters ) ;
171
+ }
172
+ }
144
173
}
0 commit comments