@@ -3103,69 +3103,44 @@ protected virtual MethodInfo GetRealMethod(ref Type type, ref object obj, string
3103
3103
. Where ( m => m . Name . Equals ( func , StringComparisonForCasing ) && m . GetParameters ( ) . Length == modifiedArgs . Count )
3104
3104
. ToList ( ) ;
3105
3105
3106
- for ( int m = 0 ; m < methodInfos . Count && methodInfo == null ; m ++ )
3106
+ // For Linq methods that are overloaded and implement possibly lambda arguments
3107
+ try
3107
3108
{
3108
- methodInfos [ m ] = MakeConcreteMethodIfGeneric ( methodInfos [ m ] , genericsTypes , inferedGenericsTypes ) ;
3109
-
3110
- bool parametersCastOK = true ;
3111
-
3112
- modifiedArgs = new List < object > ( args ) ;
3113
-
3114
- for ( int a = 0 ; a < modifiedArgs . Count ; a ++ )
3109
+ if ( methodInfos . Count > 1
3110
+ && type == typeof ( Enumerable )
3111
+ && args . Count == 2
3112
+ && args [ 1 ] is InternalDelegate internalDelegate
3113
+ && args [ 0 ] is IEnumerable enumerable
3114
+ && enumerable . GetEnumerator ( ) is IEnumerator enumerator
3115
+ && enumerator . MoveNext ( )
3116
+ && methodInfos . Any ( m => m . GetParameters ( ) . Any ( p => p . ParameterType . Name . StartsWith ( "Func" ) ) ) )
3115
3117
{
3116
- Type parameterType = methodInfos [ m ] . GetParameters ( ) [ a ] . ParameterType ;
3117
- string paramTypeName = parameterType . Name ;
3118
+ Type lambdaResultType = internalDelegate . Invoke ( enumerator . Current ) . GetType ( ) ;
3118
3119
3119
- if ( paramTypeName . StartsWith ( "Predicate" )
3120
- && modifiedArgs [ a ] is InternalDelegate )
3121
- {
3122
- InternalDelegate led = modifiedArgs [ a ] as InternalDelegate ;
3123
- modifiedArgs [ a ] = new Predicate < object > ( o => ( bool ) ( led ( new object [ ] { o } ) ) ) ;
3124
- }
3125
- else if ( paramTypeName . StartsWith ( "Func" )
3126
- && modifiedArgs [ a ] is InternalDelegate )
3120
+ methodInfo = methodInfos . Find ( m =>
3127
3121
{
3128
- InternalDelegate led = modifiedArgs [ a ] as InternalDelegate ;
3129
- DelegateEncaps de = new DelegateEncaps ( led ) ;
3130
- MethodInfo encapsMethod = de . GetType ( )
3131
- . GetMethod ( $ "Func{ parameterType . GetGenericArguments ( ) . Length - 1 } ")
3132
- . MakeGenericMethod ( parameterType . GetGenericArguments ( ) ) ;
3133
- modifiedArgs [ a ] = Delegate . CreateDelegate ( parameterType , de , encapsMethod ) ;
3134
- }
3135
- else if ( paramTypeName . StartsWith ( "Action" )
3136
- && modifiedArgs [ a ] is InternalDelegate )
3137
- {
3138
- InternalDelegate led = modifiedArgs [ a ] as InternalDelegate ;
3139
- DelegateEncaps de = new DelegateEncaps ( led ) ;
3140
- MethodInfo encapsMethod = de . GetType ( )
3141
- . GetMethod ( $ "Action{ parameterType . GetGenericArguments ( ) . Length } ")
3142
- . MakeGenericMethod ( parameterType . GetGenericArguments ( ) ) ;
3143
- modifiedArgs [ a ] = Delegate . CreateDelegate ( parameterType , de , encapsMethod ) ;
3144
- }
3145
- else if ( paramTypeName . StartsWith ( "Converter" )
3146
- && modifiedArgs [ a ] is InternalDelegate )
3122
+ ParameterInfo [ ] parameterInfos = m . GetParameters ( ) ;
3123
+
3124
+ return parameterInfos . Length == 2
3125
+ && parameterInfos [ 1 ] . ParameterType . Name . StartsWith ( "Func" )
3126
+ && parameterInfos [ 1 ] . ParameterType . GenericTypeArguments is Type [ ] genericTypesArgs
3127
+ && genericTypesArgs . Length == 2
3128
+ && genericTypesArgs [ 1 ] == lambdaResultType ;
3129
+ } ) ;
3130
+
3131
+ if ( methodInfo != null )
3147
3132
{
3148
- InternalDelegate led = modifiedArgs [ a ] as InternalDelegate ;
3149
- modifiedArgs [ a ] = new Converter < object , object > ( o => led ( new object [ ] { o } ) ) ;
3150
- }
3151
- else
3152
- {
3153
- try
3154
- {
3155
- if ( ! methodInfos [ m ] . GetParameters ( ) [ a ] . ParameterType . IsAssignableFrom ( modifiedArgs [ a ] . GetType ( ) ) )
3156
- {
3157
- modifiedArgs [ a ] = Convert . ChangeType ( modifiedArgs [ a ] , methodInfos [ m ] . GetParameters ( ) [ a ] . ParameterType ) ;
3158
- }
3159
- }
3160
- catch
3161
- {
3162
- parametersCastOK = false ;
3163
- }
3133
+ methodInfo = TryToCastMethodParametersToMakeItCallable ( methodInfo , modifiedArgs , genericsTypes , inferedGenericsTypes ) ;
3164
3134
}
3165
3135
}
3136
+ }
3137
+ catch { }
3166
3138
3167
- if ( parametersCastOK )
3168
- methodInfo = methodInfos [ m ] ;
3139
+ for ( int m = 0 ; m < methodInfos . Count && methodInfo == null ; m ++ )
3140
+ {
3141
+ modifiedArgs = new List < object > ( args ) ;
3142
+
3143
+ methodInfo = TryToCastMethodParametersToMakeItCallable ( methodInfos [ m ] , modifiedArgs , genericsTypes , inferedGenericsTypes ) ;
3169
3144
}
3170
3145
3171
3146
if ( methodInfo != null )
@@ -3178,6 +3153,73 @@ protected virtual MethodInfo GetRealMethod(ref Type type, ref object obj, string
3178
3153
return methodInfo ;
3179
3154
}
3180
3155
3156
+ protected virtual MethodInfo TryToCastMethodParametersToMakeItCallable ( MethodInfo methodInfoToCast , List < object > modifiedArgs , string genericsTypes , Type [ ] inferedGenericsTypes )
3157
+ {
3158
+ MethodInfo methodInfo = null ;
3159
+
3160
+ methodInfoToCast = MakeConcreteMethodIfGeneric ( methodInfoToCast , genericsTypes , inferedGenericsTypes ) ;
3161
+
3162
+ bool parametersCastOK = true ;
3163
+
3164
+ for ( int a = 0 ; a < modifiedArgs . Count ; a ++ )
3165
+ {
3166
+ Type parameterType = methodInfoToCast . GetParameters ( ) [ a ] . ParameterType ;
3167
+ string paramTypeName = parameterType . Name ;
3168
+
3169
+ if ( paramTypeName . StartsWith ( "Predicate" )
3170
+ && modifiedArgs [ a ] is InternalDelegate )
3171
+ {
3172
+ InternalDelegate led = modifiedArgs [ a ] as InternalDelegate ;
3173
+ modifiedArgs [ a ] = new Predicate < object > ( o => ( bool ) ( led ( new object [ ] { o } ) ) ) ;
3174
+ }
3175
+ else if ( paramTypeName . StartsWith ( "Func" )
3176
+ && modifiedArgs [ a ] is InternalDelegate )
3177
+ {
3178
+ InternalDelegate led = modifiedArgs [ a ] as InternalDelegate ;
3179
+ DelegateEncaps de = new DelegateEncaps ( led ) ;
3180
+ MethodInfo encapsMethod = de . GetType ( )
3181
+ . GetMethod ( $ "Func{ parameterType . GetGenericArguments ( ) . Length - 1 } ")
3182
+ . MakeGenericMethod ( parameterType . GetGenericArguments ( ) ) ;
3183
+ modifiedArgs [ a ] = Delegate . CreateDelegate ( parameterType , de , encapsMethod ) ;
3184
+ }
3185
+ else if ( paramTypeName . StartsWith ( "Action" )
3186
+ && modifiedArgs [ a ] is InternalDelegate )
3187
+ {
3188
+ InternalDelegate led = modifiedArgs [ a ] as InternalDelegate ;
3189
+ DelegateEncaps de = new DelegateEncaps ( led ) ;
3190
+ MethodInfo encapsMethod = de . GetType ( )
3191
+ . GetMethod ( $ "Action{ parameterType . GetGenericArguments ( ) . Length } ")
3192
+ . MakeGenericMethod ( parameterType . GetGenericArguments ( ) ) ;
3193
+ modifiedArgs [ a ] = Delegate . CreateDelegate ( parameterType , de , encapsMethod ) ;
3194
+ }
3195
+ else if ( paramTypeName . StartsWith ( "Converter" )
3196
+ && modifiedArgs [ a ] is InternalDelegate )
3197
+ {
3198
+ InternalDelegate led = modifiedArgs [ a ] as InternalDelegate ;
3199
+ modifiedArgs [ a ] = new Converter < object , object > ( o => led ( new object [ ] { o } ) ) ;
3200
+ }
3201
+ else
3202
+ {
3203
+ try
3204
+ {
3205
+ if ( ! methodInfoToCast . GetParameters ( ) [ a ] . ParameterType . IsAssignableFrom ( modifiedArgs [ a ] . GetType ( ) ) )
3206
+ {
3207
+ modifiedArgs [ a ] = Convert . ChangeType ( modifiedArgs [ a ] , methodInfoToCast . GetParameters ( ) [ a ] . ParameterType ) ;
3208
+ }
3209
+ }
3210
+ catch
3211
+ {
3212
+ parametersCastOK = false ;
3213
+ }
3214
+ }
3215
+ }
3216
+
3217
+ if ( parametersCastOK )
3218
+ methodInfo = methodInfoToCast ;
3219
+
3220
+ return methodInfo ;
3221
+ }
3222
+
3181
3223
protected virtual MethodInfo MakeConcreteMethodIfGeneric ( MethodInfo methodInfo , string genericsTypes , Type [ ] inferedGenericsTypes )
3182
3224
{
3183
3225
if ( methodInfo . IsGenericMethod )
0 commit comments