@@ -1795,19 +1795,25 @@ private Expression ParseMemberAccess(Type? type, Expression? expression, string?
1795
1795
1796
1796
if ( _textParser . CurrentToken . Id == TokenId . OpenParen )
1797
1797
{
1798
+ Expression [ ] ? args = null ;
1799
+
1798
1800
var isStaticAccess = expression == null ;
1799
1801
1800
- if ( ! isStaticAccess && type != typeof ( string ) )
1802
+ if ( ! isStaticAccess )
1801
1803
{
1802
1804
var enumerableType = TypeHelper . FindGenericType ( typeof ( IEnumerable < > ) , type ) ;
1803
1805
if ( enumerableType != null )
1804
1806
{
1805
- Type elementType = enumerableType . GetTypeInfo ( ) . GetGenericTypeArguments ( ) [ 0 ] ;
1806
- return ParseEnumerable ( expression ! , elementType , id , errorPos , type ) ;
1807
+ var elementType = enumerableType . GetTypeInfo ( ) . GetGenericTypeArguments ( ) [ 0 ] ;
1808
+ if ( TryParseEnumerable ( expression ! , elementType , id , errorPos , type , out args , out var enumerableExpression ) )
1809
+ {
1810
+ return enumerableExpression ;
1811
+ }
1807
1812
}
1808
1813
}
1809
1814
1810
- Expression [ ] args = ParseArgumentList ( ) ;
1815
+ // If args is not set by TryParseEnumerable (in case when the expression is not an Enumerable), do parse the argument list here.
1816
+ args ??= ParseArgumentList ( ) ;
1811
1817
switch ( _methodFinder . FindMethod ( type , id , isStaticAccess , ref expression , ref args , out var methodBase ) )
1812
1818
{
1813
1819
case 0 :
@@ -2038,7 +2044,7 @@ private Expression ParseAsEnumOrNestedClass(string id)
2038
2044
return ParseMemberAccess ( type , null , identifier ) ;
2039
2045
}
2040
2046
2041
- private Expression ParseEnumerable ( Expression instance , Type elementType , string methodName , int errorPos , Type ? type )
2047
+ private bool TryParseEnumerable ( Expression instance , Type elementType , string methodName , int errorPos , Type ? type , out Expression [ ] ? args , [ NotNullWhen ( true ) ] out Expression ? expression )
2042
2048
{
2043
2049
var oldParent = _parent ;
2044
2050
@@ -2057,15 +2063,24 @@ private Expression ParseEnumerable(Expression instance, Type elementType, string
2057
2063
_it = innerIt ;
2058
2064
}
2059
2065
2060
- Expression [ ] args = ParseArgumentList ( ) ;
2066
+ args = ParseArgumentList ( ) ;
2067
+
2068
+ var t = type ?? instance . Type ;
2069
+ if ( t == typeof ( string ) && _methodFinder . ContainsMethod ( t , methodName , false , instance , ref args ) )
2070
+ {
2071
+ // In case the type is a string, and does contain the methodName (like "IndexOf"), then return false to indicate that the methodName is not an Enumerable method.
2072
+ expression = null ;
2073
+ return false ;
2074
+ }
2061
2075
2062
2076
_it = outerIt ;
2063
2077
_parent = oldParent ;
2064
2078
2065
2079
if ( type != null && TypeHelper . IsDictionary ( type ) && _methodFinder . ContainsMethod ( type , methodName , false ) )
2066
2080
{
2067
2081
var dictionaryMethod = type . GetMethod ( methodName ) ! ;
2068
- return Expression . Call ( instance , dictionaryMethod , args ) ;
2082
+ expression = Expression . Call ( instance , dictionaryMethod , args ) ;
2083
+ return true ;
2069
2084
}
2070
2085
2071
2086
// #794 - Check if the method is an aggregate (Average or Sum) method and try to update the arguments to match the method arguments
@@ -2139,7 +2154,8 @@ private Expression ParseEnumerable(Expression instance, Type elementType, string
2139
2154
}
2140
2155
}
2141
2156
2142
- return Expression . Call ( callType , methodName , typeArgs , args ) ;
2157
+ expression = Expression . Call ( callType , methodName , typeArgs , args ) ;
2158
+ return true ;
2143
2159
}
2144
2160
2145
2161
private Type ResolveTypeFromArgumentExpression ( string functionName , Expression argumentExpression , int ? arguments = null )
0 commit comments