@@ -1377,7 +1377,17 @@ private Expression ParseMethodGroupInvocation(MethodGroupExpression methodGroup,
13771377 applicableMethods = MethodResolution . FindBestMethod ( candidates . Select ( _ => _ . InvokeMethod ) , args ) ;
13781378
13791379 if ( applicableMethods . Length == 0 )
1380+ {
1381+ if ( args . Any ( IsDynamicExpression ) )
1382+ {
1383+ // TODO: we could try to find the best method by using the dynamic binder
1384+ var candidatesWithSameArgumentCount = candidates . Where ( _ => _ . Method . GetParameters ( ) . Length == args . Length ) . ToList ( ) ;
1385+ if ( candidatesWithSameArgumentCount . Count == 1 )
1386+ return ParseDynamicMethodGroupInvocation ( candidatesWithSameArgumentCount [ 0 ] . Delegate , args ) ;
1387+ }
1388+
13801389 throw ParseException . Create ( errorPos , ErrorMessages . ArgsIncompatibleWithDelegate ) ;
1390+ }
13811391
13821392 if ( applicableMethods . Length > 1 )
13831393 throw ParseException . Create ( errorPos , ErrorMessages . AmbiguousDelegateInvocation ) ;
@@ -1691,21 +1701,21 @@ private Expression ParseMethodInvocation(Type type, Expression instance, int err
16911701 if ( methodInvocationExpression != null )
16921702 return methodInvocationExpression ;
16931703
1694- if ( TypeUtils . IsDynamicType ( type ) || IsDynamicExpression ( instance ) )
1704+ if ( TypeUtils . IsDynamicType ( type ) || IsDynamicExpression ( instance ) || args . Any ( IsDynamicExpression ) )
16951705 return ParseDynamicMethodInvocation ( type , instance , methodName , args ) ;
16961706
16971707 throw new NoApplicableMethodException ( methodName , TypeUtils . GetTypeName ( type ) , errorPos ) ;
16981708 }
16991709
1700- private Expression ParseExtensionMethodInvocation ( Type type , Expression instance , int errorPos , string id , Expression [ ] args )
1710+ private Expression ParseExtensionMethodInvocation ( Type type , Expression instance , int errorPos , string methodName , Expression [ ] args )
17011711 {
17021712 var extensionMethodsArguments = new Expression [ args . Length + 1 ] ;
17031713 extensionMethodsArguments [ 0 ] = instance ;
17041714 args . CopyTo ( extensionMethodsArguments , 1 ) ;
17051715
1706- var extensionMethods = _memberFinder . FindExtensionMethods ( id , extensionMethodsArguments ) ;
1716+ var extensionMethods = _memberFinder . FindExtensionMethods ( methodName , extensionMethodsArguments ) ;
17071717 if ( extensionMethods . Length > 1 )
1708- throw ParseException . Create ( errorPos , ErrorMessages . AmbiguousMethodInvocation , id , TypeUtils . GetTypeName ( type ) ) ;
1718+ throw ParseException . Create ( errorPos , ErrorMessages . AmbiguousMethodInvocation , methodName , TypeUtils . GetTypeName ( type ) ) ;
17091719
17101720 if ( extensionMethods . Length == 1 )
17111721 {
@@ -1719,11 +1729,11 @@ private Expression ParseExtensionMethodInvocation(Type type, Expression instance
17191729 return null ;
17201730 }
17211731
1722- private Expression ParseNormalMethodInvocation ( Type type , Expression instance , int errorPos , string id , Expression [ ] args )
1732+ private Expression ParseNormalMethodInvocation ( Type type , Expression instance , int errorPos , string methodName , Expression [ ] args )
17231733 {
1724- var applicableMethods = _memberFinder . FindMethods ( type , id , instance == null , args ) ;
1734+ var applicableMethods = _memberFinder . FindMethods ( type , methodName , instance == null , args ) ;
17251735 if ( applicableMethods . Length > 1 )
1726- throw ParseException . Create ( errorPos , ErrorMessages . AmbiguousMethodInvocation , id , TypeUtils . GetTypeName ( type ) ) ;
1736+ throw ParseException . Create ( errorPos , ErrorMessages . AmbiguousMethodInvocation , methodName , TypeUtils . GetTypeName ( type ) ) ;
17271737
17281738 if ( applicableMethods . Length == 1 )
17291739 {
@@ -1791,8 +1801,16 @@ private static Expression ParseDynamicProperty(Type type, Expression instance, s
17911801 private static Expression ParseDynamicMethodInvocation ( Type type , Expression instance , string methodName , Expression [ ] args )
17921802 {
17931803 var argsDynamic = args . ToList ( ) ;
1794- argsDynamic . Insert ( 0 , instance ) ;
1795- return Expression . Dynamic ( new LateInvokeMethodCallSiteBinder ( methodName ) , typeof ( object ) , argsDynamic ) ;
1804+ var isStatic = instance == null ;
1805+ argsDynamic . Insert ( 0 , ! isStatic ? instance : Expression . Constant ( type ) ) ;
1806+ return Expression . Dynamic ( new LateInvokeMethodCallSiteBinder ( methodName , isStatic ) , typeof ( object ) , argsDynamic ) ;
1807+ }
1808+
1809+ private Expression ParseDynamicMethodGroupInvocation ( Delegate @delegate , Expression [ ] args )
1810+ {
1811+ var argsDynamic = args . ToList ( ) ;
1812+ argsDynamic . Insert ( 0 , Expression . Constant ( @delegate ) ) ;
1813+ return Expression . Dynamic ( new LateInvokeDelegateCallSiteBinder ( ) , typeof ( object ) , argsDynamic ) ;
17961814 }
17971815
17981816 private static Expression ParseDynamicIndex ( Type type , Expression instance , Expression [ ] args )
0 commit comments