@@ -101,7 +101,7 @@ private Expression ParseExpressionSegment(Type returnType)
101
101
int errorPos = _token . pos ;
102
102
var expression = ParseExpressionSegment ( ) ;
103
103
104
- if ( returnType != typeof ( void ) && returnType != typeof ( object ) )
104
+ if ( returnType != typeof ( void ) )
105
105
{
106
106
return GenerateConversion ( expression , returnType , errorPos ) ;
107
107
}
@@ -1126,31 +1126,41 @@ private MemberBinding[] ParseMemberInitializerList(Type newType)
1126
1126
}
1127
1127
1128
1128
private Expression ParseLambdaInvocation ( LambdaExpression lambda , int errorPos )
1129
+ {
1130
+ return ParseInvocation ( lambda , errorPos , ErrorMessages . ArgsIncompatibleWithLambda ) ;
1131
+ }
1132
+
1133
+ private Expression ParseDelegateInvocation ( Expression delegateExp , int errorPos )
1134
+ {
1135
+ return ParseInvocation ( delegateExp , errorPos , ErrorMessages . ArgsIncompatibleWithDelegate ) ;
1136
+ }
1137
+
1138
+ private Expression ParseInvocation ( Expression expr , int errorPos , string error )
1129
1139
{
1130
1140
var args = ParseArgumentList ( ) ;
1131
1141
1132
- if ( ! PrepareDelegateInvoke ( lambda . Type , ref args ) )
1133
- throw CreateParseException ( errorPos , ErrorMessages . ArgsIncompatibleWithLambda ) ;
1142
+ var invokeMethod = FindInvokeMethod ( expr . Type ) ;
1143
+ if ( invokeMethod == null || ! CheckIfMethodIsApplicableAndPrepareIt ( invokeMethod , args ) )
1144
+ throw CreateParseException ( errorPos , error ) ;
1134
1145
1135
- return Expression . Invoke ( lambda , args ) ;
1146
+ return Expression . Invoke ( expr , invokeMethod . PromotedParameters ) ;
1136
1147
}
1137
1148
1138
1149
private Expression ParseMethodGroupInvocation ( MethodGroupExpression methodGroup , int errorPos )
1139
1150
{
1140
1151
var args = ParseArgumentList ( ) ;
1141
1152
1142
1153
// find the best delegates that can be used with the provided arguments
1143
- var flags = BindingFlags . Public | BindingFlags . Instance ;
1144
1154
var candidates = methodGroup . Overloads
1145
1155
. Select ( _ => new
1146
1156
{
1147
1157
Delegate = _ ,
1148
1158
Method = _ . Method ,
1149
- InvokeMethods = _ . GetType ( ) . FindMembers ( MemberTypes . Method , flags , Type . FilterName , "Invoke" ) . Cast < MethodInfo > ( ) ,
1159
+ InvokeMethod = FindInvokeMethod ( _ . GetType ( ) ) ,
1150
1160
} )
1151
1161
. ToList ( ) ;
1152
1162
1153
- var applicableMethods = FindBestMethod ( candidates . SelectMany ( _ => _ . InvokeMethods ) , args ) ;
1163
+ var applicableMethods = FindBestMethod ( candidates . Select ( _ => _ . InvokeMethod ) , args ) ;
1154
1164
1155
1165
// no method found: retry with the delegate's method
1156
1166
// (the parameters might be different, e.g. params array, default value, etc)
@@ -1164,31 +1174,10 @@ private Expression ParseMethodGroupInvocation(MethodGroupExpression methodGroup,
1164
1174
throw CreateParseException ( errorPos , ErrorMessages . AmbiguousDelegateInvocation ) ;
1165
1175
1166
1176
var applicableMethod = applicableMethods [ 0 ] ;
1167
- var usedDeledate = candidates . Single ( _ => new [ ] { _ . Method } . Concat ( _ . InvokeMethods ) . Any ( m => m == applicableMethod . MethodBase ) ) . Delegate ;
1177
+ var usedDeledate = candidates . Single ( _ => new [ ] { _ . Method , _ . InvokeMethod ? . MethodBase } . Any ( m => m == applicableMethod . MethodBase ) ) . Delegate ;
1168
1178
return Expression . Invoke ( Expression . Constant ( usedDeledate ) , applicableMethod . PromotedParameters ) ;
1169
1179
}
1170
1180
1171
- private Expression ParseDelegateInvocation ( Expression delegateExp , int errorPos )
1172
- {
1173
- var args = ParseArgumentList ( ) ;
1174
-
1175
- if ( ! PrepareDelegateInvoke ( delegateExp . Type , ref args ) )
1176
- throw CreateParseException ( errorPos , ErrorMessages . ArgsIncompatibleWithDelegate ) ;
1177
-
1178
- return Expression . Invoke ( delegateExp , args ) ;
1179
- }
1180
-
1181
- private bool PrepareDelegateInvoke ( Type type , ref Expression [ ] args )
1182
- {
1183
- var applicableMethods = FindMethods ( type , "Invoke" , false , args ) ;
1184
- if ( applicableMethods . Length != 1 )
1185
- return false ;
1186
-
1187
- args = applicableMethods [ 0 ] . PromotedParameters ;
1188
-
1189
- return true ;
1190
- }
1191
-
1192
1181
private Type ParseKnownType ( )
1193
1182
{
1194
1183
var name = _token . text ;
@@ -1797,6 +1786,23 @@ private MethodData[] FindMethods(Type type, string methodName, bool staticAccess
1797
1786
return new MethodData [ 0 ] ;
1798
1787
}
1799
1788
1789
+ private MethodData FindInvokeMethod ( Type type )
1790
+ {
1791
+ var flags = BindingFlags . Public | BindingFlags . DeclaredOnly |
1792
+ BindingFlags . Instance | _bindingCase ;
1793
+ foreach ( var t in SelfAndBaseTypes ( type ) )
1794
+ {
1795
+ var method = t . FindMembers ( MemberTypes . Method , flags , _memberFilterCase , "Invoke" )
1796
+ . Cast < MethodBase > ( )
1797
+ . SingleOrDefault ( ) ;
1798
+
1799
+ if ( method != null )
1800
+ return MethodData . Gen ( method ) ;
1801
+ }
1802
+
1803
+ return null ;
1804
+ }
1805
+
1800
1806
private MethodData [ ] FindExtensionMethods ( string methodName , Expression [ ] args )
1801
1807
{
1802
1808
var matchMethods = _arguments . GetExtensionMethods ( methodName ) ;
0 commit comments