@@ -1928,7 +1928,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
1928
1928
object obj = inObject ? stack . Pop ( ) : Context ;
1929
1929
object keepObj = obj ;
1930
1930
Type objType = null ;
1931
- Type [ ] inferedGenericsTypes = null ; // obj?.GetType().GenericTypeArguments;
1931
+ Type [ ] inferedGenericsTypes = obj ? . GetType ( ) . GenericTypeArguments ;
1932
1932
ValueTypeNestingTrace valueTypeNestingTrace = null ;
1933
1933
1934
1934
if ( obj != null && TypesToBlock . Contains ( obj . GetType ( ) ) )
@@ -3424,53 +3424,76 @@ protected virtual MethodInfo TryToCastMethodParametersToMakeItCallable(MethodInf
3424
3424
3425
3425
MethodInfo oldMethodInfo = methodInfoToCast ;
3426
3426
3427
- if ( methodInfoToCast . IsGenericMethod
3428
- && methodInfoToCast . ContainsGenericParameters )
3427
+ if ( ! string . IsNullOrEmpty ( genericsTypes ) )
3429
3428
{
3430
- if ( ! string . IsNullOrWhiteSpace ( genericsTypes ) )
3431
- {
3432
- methodInfoToCast = MakeConcreteMethodIfGeneric ( methodInfoToCast , genericsTypes , inferedGenericsTypes ) ;
3433
- }
3434
- else
3435
- {
3436
- Type [ ] genericArgsTypes = methodInfoToCast . GetGenericArguments ( ) ;
3437
- List < Type > inferedTypes = new List < Type > ( ) ;
3429
+ methodInfoToCast = MakeConcreteMethodIfGeneric ( methodInfoToCast , genericsTypes , inferedGenericsTypes ) ;
3430
+ }
3431
+ else if ( oldMethodInfo . IsGenericMethod
3432
+ && oldMethodInfo . ContainsGenericParameters )
3433
+ {
3434
+ Type [ ] genericArgsTypes = oldMethodInfo . GetGenericArguments ( ) ;
3435
+ List < Type > inferedTypes = new List < Type > ( ) ;
3438
3436
3439
- for ( int t = 0 ; t < genericArgsTypes . Length ; t ++ )
3437
+ for ( int t = 0 ; t < genericArgsTypes . Length ; t ++ )
3438
+ {
3439
+ if ( genericArgsTypes [ t ] . IsGenericParameter )
3440
3440
{
3441
- if ( genericArgsTypes [ t ] . IsGenericParameter )
3442
- {
3443
- string name = genericArgsTypes [ t ] . Name ;
3444
- ParameterInfo [ ] parameterInfos = methodInfoToCast . GetParameters ( ) ;
3445
-
3446
- ParameterInfo paramsForInference = Array . Find ( parameterInfos , p => p . ParameterType . IsGenericParameter
3447
- && p . ParameterType . Name . Equals ( name )
3448
- && modifiedArgs . Count > p . Position
3449
- && ! modifiedArgs [ p . Position ] . GetType ( ) . IsGenericParameter ) ;
3441
+ string name = genericArgsTypes [ t ] . Name ;
3442
+ ParameterInfo [ ] parameterInfos = oldMethodInfo . GetParameters ( ) ;
3450
3443
3451
- if ( paramsForInference != null )
3452
- {
3453
- inferedTypes . Add ( modifiedArgs [ paramsForInference . Position ] . GetType ( ) ) ;
3454
- }
3455
- else
3456
- {
3457
- paramsForInference = Array . Find ( parameterInfos , p => p . ParameterType . IsGenericType
3458
- && p . ParameterType . ContainsGenericParameters
3459
- && p . ParameterType . GetGenericArguments ( ) . Any ( subP => subP . Name . Equals ( name ) )
3460
- && modifiedArgs . Count > p . Position
3461
- && ! modifiedArgs [ p . Position ] . GetType ( ) . IsGenericType ) ;
3444
+ ParameterInfo paramsForInference = Array . Find ( parameterInfos , p => p . ParameterType . IsGenericParameter
3445
+ && p . ParameterType . Name . Equals ( name )
3446
+ && modifiedArgs . Count > p . Position
3447
+ && ! modifiedArgs [ p . Position ] . GetType ( ) . IsGenericParameter ) ;
3462
3448
3463
- inferedTypes . Add ( modifiedArgs [ paramsForInference . Position ] . GetType ( ) . GetElementType ( ) ) ;
3464
- }
3449
+ if ( paramsForInference != null )
3450
+ {
3451
+ inferedTypes . Add ( modifiedArgs [ paramsForInference . Position ] . GetType ( ) ) ;
3465
3452
}
3466
3453
else
3467
3454
{
3468
- inferedTypes . Add ( genericArgsTypes [ t ] ) ;
3455
+ paramsForInference = Array . Find ( parameterInfos , p => p . ParameterType . IsGenericType
3456
+ && p . ParameterType . ContainsGenericParameters
3457
+ && p . ParameterType . GetGenericArguments ( ) . Any ( subP => subP . Name . Equals ( name ) )
3458
+ && modifiedArgs . Count > p . Position
3459
+ && ! modifiedArgs [ p . Position ] . GetType ( ) . IsGenericType ) ;
3460
+
3461
+ if ( paramsForInference != null )
3462
+ {
3463
+ if ( modifiedArgs [ paramsForInference . Position ] is MethodsGroupEncaps methodsGroupEncaps )
3464
+ {
3465
+ if ( paramsForInference . ParameterType . Name . StartsWith ( "Converter" ) )
3466
+ {
3467
+ Type specificType = paramsForInference . ParameterType . GetGenericArguments ( ) . FirstOrDefault ( pType => pType . Name . Equals ( name ) ) ;
3468
+ MethodInfo paraMethodInfo = methodsGroupEncaps . MethodsGroup . FirstOrDefault ( mi => mi . GetParameters ( ) . Length == 1 ) ;
3469
+ if ( specificType != null && specificType . GenericParameterPosition == 0 )
3470
+ {
3471
+ inferedTypes . Add ( paraMethodInfo . GetParameters ( ) [ 0 ] . ParameterType ) ;
3472
+ }
3473
+ else if ( specificType != null && specificType . GenericParameterPosition == 1 )
3474
+ {
3475
+ inferedTypes . Add ( paraMethodInfo . ReturnType ) ;
3476
+ }
3477
+ }
3478
+ }
3479
+ else
3480
+ {
3481
+ if ( modifiedArgs [ paramsForInference . Position ] . GetType ( ) . HasElementType )
3482
+ inferedTypes . Add ( modifiedArgs [ paramsForInference . Position ] . GetType ( ) . GetElementType ( ) ) ;
3483
+ }
3484
+ }
3469
3485
}
3470
3486
}
3471
-
3472
- methodInfoToCast = MakeConcreteMethodIfGeneric ( methodInfoToCast , genericsTypes , inferedTypes . ToArray ( ) ) ;
3487
+ else
3488
+ {
3489
+ inferedTypes . Add ( genericArgsTypes [ t ] ) ;
3490
+ }
3473
3491
}
3492
+
3493
+ if ( inferedTypes . Count > 0 && inferedTypes . Count == genericArgsTypes . Length )
3494
+ methodInfoToCast = MakeConcreteMethodIfGeneric ( oldMethodInfo , genericsTypes , inferedTypes . ToArray ( ) ) ;
3495
+ else
3496
+ methodInfoToCast = MakeConcreteMethodIfGeneric ( methodInfoToCast , genericsTypes , inferedGenericsTypes ) ;
3474
3497
}
3475
3498
3476
3499
bool parametersCastOK = true ;
0 commit comments