15
15
using System . Globalization ;
16
16
using System . Linq ;
17
17
using System . Reflection ;
18
+ using System . Runtime . CompilerServices ;
18
19
using System . Runtime . InteropServices ;
19
20
using System . Text ;
20
21
using System . Text . RegularExpressions ;
21
- using System . Runtime . CompilerServices ;
22
22
23
23
namespace CodingSeb . ExpressionEvaluator
24
24
{
@@ -1772,7 +1772,7 @@ void InitSimpleObjet(object element, List<string> initArgs)
1772
1772
int typeIndex = 0 ;
1773
1773
Type type = EvaluateType ( completeName + genericTypes , ref typeIndex ) ;
1774
1774
1775
- if ( type == null || typeIndex > 0 && typeIndex < completeName . Length )
1775
+ if ( type == null || ( typeIndex > 0 && typeIndex < completeName . Length ) )
1776
1776
throw new ExpressionEvaluatorSyntaxErrorException ( $ "Type or class { completeName } { genericTypes } is unknown") ;
1777
1777
1778
1778
void Init ( object element , List < string > initArgs )
@@ -2159,6 +2159,22 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
2159
2159
{
2160
2160
stack . Push ( delegateVar . DynamicInvoke ( funcArgs . ConvertAll ( Evaluate ) . ToArray ( ) ) ) ;
2161
2161
}
2162
+ else if ( Variables . TryGetValue ( varFuncName , out o ) && o is MethodsGroupEncaps methodsGroupEncaps )
2163
+ {
2164
+ List < object > args = funcArgs . ConvertAll ( Evaluate ) ;
2165
+ List < object > modifiedArgs = null ;
2166
+ MethodInfo methodInfo = null ;
2167
+
2168
+ for ( int m = 0 ; methodInfo == null && m < methodsGroupEncaps . MethodsGroup . Length ; m ++ )
2169
+ {
2170
+ modifiedArgs = new List < object > ( args ) ;
2171
+
2172
+ methodInfo = TryToCastMethodParametersToMakeItCallable ( methodsGroupEncaps . MethodsGroup [ m ] , modifiedArgs , genericsTypes , new Type [ 0 ] , methodsGroupEncaps . ContainerObject ) ;
2173
+ }
2174
+
2175
+ if ( methodInfo != null )
2176
+ stack . Push ( methodInfo . Invoke ( methodsGroupEncaps . ContainerObject , modifiedArgs ? . ToArray ( ) ) ) ;
2177
+ }
2162
2178
else
2163
2179
{
2164
2180
FunctionEvaluationEventArg functionEvaluationEventArg = new FunctionEvaluationEventArg ( varFuncName , funcArgs , this , genericTypes : genericsTypes , evaluateGenericTypes : GetConcreteTypes ) ;
@@ -2249,6 +2265,20 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
2249
2265
if ( member == null && ! isDynamic )
2250
2266
member = objType . GetField ( varFuncName , flag ) ;
2251
2267
2268
+ if ( member == null && ! isDynamic )
2269
+ {
2270
+ MethodInfo [ ] methodsGroup = objType . GetMember ( varFuncName , flag ) . OfType < MethodInfo > ( ) . ToArray ( ) ;
2271
+
2272
+ if ( methodsGroup . Length > 0 )
2273
+ {
2274
+ varValue = new MethodsGroupEncaps ( )
2275
+ {
2276
+ ContainerObject = obj ,
2277
+ MethodsGroup = methodsGroup
2278
+ } ;
2279
+ }
2280
+ }
2281
+
2252
2282
bool pushVarValue = true ;
2253
2283
2254
2284
if ( isDynamic )
@@ -3392,6 +3422,8 @@ protected virtual MethodInfo TryToCastMethodParametersToMakeItCallable(MethodInf
3392
3422
{
3393
3423
MethodInfo methodInfo = null ;
3394
3424
3425
+ MethodInfo oldMethodInfo = methodInfoToCast ;
3426
+
3395
3427
methodInfoToCast = MakeConcreteMethodIfGeneric ( methodInfoToCast , genericsTypes , inferedGenericsTypes ) ;
3396
3428
3397
3429
bool parametersCastOK = true ;
@@ -3416,7 +3448,7 @@ protected virtual MethodInfo TryToCastMethodParametersToMakeItCallable(MethodInf
3416
3448
&& modifiedArgs [ a ] is InternalDelegate )
3417
3449
{
3418
3450
InternalDelegate led = modifiedArgs [ a ] as InternalDelegate ;
3419
- modifiedArgs [ a ] = new Predicate < object > ( o => ( bool ) ( led ( new object [ ] { o } ) ) ) ;
3451
+ modifiedArgs [ a ] = new Predicate < object > ( o => ( bool ) led ( new object [ ] { o } ) ) ;
3420
3452
}
3421
3453
else if ( paramTypeName . StartsWith ( "Func" )
3422
3454
&& modifiedArgs [ a ] is InternalDelegate )
@@ -3444,6 +3476,49 @@ protected virtual MethodInfo TryToCastMethodParametersToMakeItCallable(MethodInf
3444
3476
InternalDelegate led = modifiedArgs [ a ] as InternalDelegate ;
3445
3477
modifiedArgs [ a ] = new Converter < object , object > ( o => led ( new object [ ] { o } ) ) ;
3446
3478
}
3479
+ else if ( typeof ( Delegate ) . IsAssignableFrom ( parameterType )
3480
+ && modifiedArgs [ a ] is MethodsGroupEncaps methodsGroupEncaps )
3481
+ {
3482
+ MethodInfo invokeMethod = parameterType . GetMethod ( "Invoke" ) ;
3483
+ MethodInfo methodForDelegate = Array . Find ( methodsGroupEncaps . MethodsGroup , m => invokeMethod . GetParameters ( ) . Length == m . GetParameters ( ) . Length && invokeMethod . ReturnType . IsAssignableFrom ( m . ReturnType ) ) ;
3484
+ if ( methodForDelegate != null )
3485
+ {
3486
+ Type [ ] parametersTypes = methodForDelegate . GetParameters ( ) . Select ( p => p . ParameterType ) . ToArray ( ) ;
3487
+ Type delegateType ;
3488
+
3489
+ if ( methodForDelegate . ReturnType == typeof ( void ) )
3490
+ {
3491
+ delegateType = Type . GetType ( $ "System.Action`{ parametersTypes . Length } ") ;
3492
+ }
3493
+ else if ( paramTypeName . StartsWith ( "Predicate" ) )
3494
+ {
3495
+ delegateType = typeof ( Predicate < > ) ;
3496
+ parametersTypes = parametersTypes . Concat ( new Type [ ] { typeof ( bool ) } ) . ToArray ( ) ;
3497
+ }
3498
+ else if ( paramTypeName . StartsWith ( "Converter" ) )
3499
+ {
3500
+ delegateType = typeof ( Converter < , > ) ;
3501
+ parametersTypes = parametersTypes . Concat ( new Type [ ] { methodForDelegate . ReturnType } ) . ToArray ( ) ;
3502
+ }
3503
+ else
3504
+ {
3505
+ delegateType = Type . GetType ( $ "System.Func`{ parametersTypes . Length + 1 } ") ;
3506
+ parametersTypes = parametersTypes . Concat ( new Type [ ] { methodForDelegate . ReturnType } ) . ToArray ( ) ;
3507
+ }
3508
+
3509
+ delegateType = delegateType . MakeGenericType ( parametersTypes ) ;
3510
+
3511
+ modifiedArgs [ a ] = Delegate . CreateDelegate ( delegateType , methodsGroupEncaps . ContainerObject , methodForDelegate ) ;
3512
+
3513
+ if ( oldMethodInfo . IsGenericMethod &&
3514
+ methodInfoToCast . GetGenericArguments ( ) . Length == parametersTypes . Length &&
3515
+ ! methodInfoToCast . GetGenericArguments ( ) . SequenceEqual ( parametersTypes ) &&
3516
+ string . IsNullOrWhiteSpace ( genericsTypes ) )
3517
+ {
3518
+ methodInfoToCast = MakeConcreteMethodIfGeneric ( oldMethodInfo , genericsTypes , parametersTypes ) ;
3519
+ }
3520
+ }
3521
+ }
3447
3522
else
3448
3523
{
3449
3524
try
@@ -3478,7 +3553,7 @@ protected virtual MethodInfo TryToCastMethodParametersToMakeItCallable(MethodInf
3478
3553
{
3479
3554
try
3480
3555
{
3481
- ParameterCastEvaluationEventArg parameterCastEvaluationEventArg = new ParameterCastEvaluationEventArg ( methodInfo , parameterType , modifiedArgs [ a ] , a , this , onInstance ) ;
3556
+ ParameterCastEvaluationEventArg parameterCastEvaluationEventArg = new ParameterCastEvaluationEventArg ( methodInfoToCast , parameterType , modifiedArgs [ a ] , a , this , onInstance ) ;
3482
3557
3483
3558
EvaluateParameterCast ? . Invoke ( this , parameterCastEvaluationEventArg ) ;
3484
3559
@@ -4299,6 +4374,13 @@ public SubExpression(string expression)
4299
4374
}
4300
4375
}
4301
4376
4377
+ public partial class MethodsGroupEncaps
4378
+ {
4379
+ public object ContainerObject { get ; set ; }
4380
+
4381
+ public MethodInfo [ ] MethodsGroup { get ; set ; }
4382
+ }
4383
+
4302
4384
public partial class BubbleExceptionContainer
4303
4385
{
4304
4386
public Exception Exception { get ; set ; }
0 commit comments