@@ -49,17 +49,15 @@ public static Expression Parse(ParserArguments arguments)
49
49
private char _parseChar ;
50
50
private Token _token ;
51
51
52
- private readonly BindingFlags _bindingCase ;
53
- private readonly MemberFilter _memberFilterCase ;
52
+ private readonly MemberFinder _memberFinder ;
54
53
55
54
private readonly DefaultNumberType _defaultNumberType ;
56
55
57
56
private Parser ( ParserArguments arguments )
58
57
{
59
58
_arguments = arguments ;
60
59
61
- _bindingCase = arguments . Settings . CaseInsensitive ? BindingFlags . IgnoreCase : BindingFlags . Default ;
62
- _memberFilterCase = arguments . Settings . CaseInsensitive ? Type . FilterNameIgnoreCase : Type . FilterName ;
60
+ _memberFinder = new MemberFinder ( arguments ) ;
63
61
64
62
_defaultNumberType = arguments . Settings . DefaultNumberType ;
65
63
@@ -667,7 +665,7 @@ private MethodData FindUnaryOperator(string operatorName, Expression expr)
667
665
var args = new [ ] { expr } ;
668
666
669
667
// try to find the user defined operator on both operands
670
- var applicableMethods = FindMethods ( type , operatorName , true , args ) ;
668
+ var applicableMethods = _memberFinder . FindMethods ( type , operatorName , true , args ) ;
671
669
if ( applicableMethods . Length > 1 )
672
670
throw ParseException . Create ( errorPos , ErrorMessages . AmbiguousUnaryOperatorInvocation , operatorName , TypeUtils . GetTypeName ( type ) ) ;
673
671
@@ -1253,7 +1251,7 @@ private void ParsePossibleMemberBinding(Type newType, int originalPos, List<Memb
1253
1251
ValidateToken ( TokenId . Identifier , ErrorMessages . IdentifierExpected ) ;
1254
1252
1255
1253
var propertyOrFieldName = _token . text ;
1256
- var member = FindPropertyOrField ( newType , propertyOrFieldName , false ) ;
1254
+ var member = _memberFinder . FindPropertyOrField ( newType , propertyOrFieldName , false ) ;
1257
1255
var pos = _token . pos ;
1258
1256
if ( allowCollectionInit )
1259
1257
{
@@ -1352,7 +1350,7 @@ private Expression ParseInvocation(Expression expr, int errorPos, string error)
1352
1350
{
1353
1351
var args = ParseArgumentList ( ) ;
1354
1352
1355
- var invokeMethod = FindInvokeMethod ( expr . Type ) ;
1353
+ var invokeMethod = _memberFinder . FindInvokeMethod ( expr . Type ) ;
1356
1354
if ( invokeMethod == null || ! MethodResolution . CheckIfMethodIsApplicableAndPrepareIt ( invokeMethod , args ) )
1357
1355
throw ParseException . Create ( errorPos , error ) ;
1358
1356
@@ -1369,7 +1367,7 @@ private Expression ParseMethodGroupInvocation(MethodGroupExpression methodGroup,
1369
1367
{
1370
1368
Delegate = _ ,
1371
1369
Method = _ . Method ,
1372
- InvokeMethod = FindInvokeMethod ( _ . GetType ( ) ) ,
1370
+ InvokeMethod = _memberFinder . FindInvokeMethod ( _ . GetType ( ) ) ,
1373
1371
} )
1374
1372
. ToList ( ) ;
1375
1373
@@ -1657,7 +1655,7 @@ private Expression ParseMemberAccess(Type type, Expression instance)
1657
1655
1658
1656
private Expression GeneratePropertyOrFieldExpression ( Type type , Expression instance , int errorPos , string propertyOrFieldName )
1659
1657
{
1660
- var member = FindPropertyOrField ( type , propertyOrFieldName , instance == null ) ;
1658
+ var member = _memberFinder . FindPropertyOrField ( type , propertyOrFieldName , instance == null ) ;
1661
1659
if ( member != null )
1662
1660
{
1663
1661
return member is PropertyInfo ?
@@ -1702,7 +1700,7 @@ private Expression ParseExtensionMethodInvocation(Type type, Expression instance
1702
1700
extensionMethodsArguments [ 0 ] = instance ;
1703
1701
args . CopyTo ( extensionMethodsArguments , 1 ) ;
1704
1702
1705
- var extensionMethods = FindExtensionMethods ( id , extensionMethodsArguments ) ;
1703
+ var extensionMethods = _memberFinder . FindExtensionMethods ( id , extensionMethodsArguments ) ;
1706
1704
if ( extensionMethods . Length > 1 )
1707
1705
throw ParseException . Create ( errorPos , ErrorMessages . AmbiguousMethodInvocation , id , TypeUtils . GetTypeName ( type ) ) ;
1708
1706
@@ -1720,7 +1718,7 @@ private Expression ParseExtensionMethodInvocation(Type type, Expression instance
1720
1718
1721
1719
private Expression ParseNormalMethodInvocation ( Type type , Expression instance , int errorPos , string id , Expression [ ] args )
1722
1720
{
1723
- var applicableMethods = FindMethods ( type , id , instance == null , args ) ;
1721
+ var applicableMethods = _memberFinder . FindMethods ( type , id , instance == null , args ) ;
1724
1722
if ( applicableMethods . Length > 1 )
1725
1723
throw ParseException . Create ( errorPos , ErrorMessages . AmbiguousMethodInvocation , id , TypeUtils . GetTypeName ( type ) ) ;
1726
1724
@@ -1850,7 +1848,7 @@ private Expression ParseElementAccess(Expression expr)
1850
1848
if ( TypeUtils . IsDynamicType ( expr . Type ) || IsDynamicExpression ( expr ) )
1851
1849
return ParseDynamicIndex ( expr . Type , expr , args ) ;
1852
1850
1853
- var applicableMethods = FindIndexer ( expr . Type , args ) ;
1851
+ var applicableMethods = _memberFinder . FindIndexer ( expr . Type , args ) ;
1854
1852
if ( applicableMethods . Length == 0 )
1855
1853
{
1856
1854
throw ParseException . Create ( errorPos , ErrorMessages . NoApplicableIndexer ,
@@ -1907,122 +1905,13 @@ private void CheckAndPromoteOperands(Type signatures, ref Expression left, ref E
1907
1905
1908
1906
private Expression [ ] PrepareOperandArguments ( Type signatures , Expression [ ] args )
1909
1907
{
1910
- var applicableMethods = FindMethods ( signatures , "F" , false , args ) ;
1908
+ var applicableMethods = _memberFinder . FindMethods ( signatures , "F" , false , args ) ;
1911
1909
if ( applicableMethods . Length == 1 )
1912
1910
return applicableMethods [ 0 ] . PromotedParameters ;
1913
1911
1914
1912
return args ;
1915
1913
}
1916
1914
1917
- private MemberInfo FindPropertyOrField ( Type type , string memberName , bool staticAccess )
1918
- {
1919
- var flags = BindingFlags . Public | BindingFlags . DeclaredOnly |
1920
- ( staticAccess ? BindingFlags . Static : BindingFlags . Instance ) | _bindingCase ;
1921
-
1922
- foreach ( var t in SelfAndBaseTypes ( type ) )
1923
- {
1924
- var members = t . FindMembers ( MemberTypes . Property | MemberTypes . Field , flags , _memberFilterCase , memberName ) ;
1925
- if ( members . Length != 0 )
1926
- return members [ 0 ] ;
1927
- }
1928
- return null ;
1929
- }
1930
-
1931
- private MethodData [ ] FindMethods ( Type type , string methodName , bool staticAccess , Expression [ ] args )
1932
- {
1933
- var flags = BindingFlags . Public | BindingFlags . DeclaredOnly |
1934
- ( staticAccess ? BindingFlags . Static : BindingFlags . Instance ) | _bindingCase ;
1935
- foreach ( var t in SelfAndBaseTypes ( type ) )
1936
- {
1937
- var members = t . FindMembers ( MemberTypes . Method , flags , _memberFilterCase , methodName ) ;
1938
- var applicableMethods = MethodResolution . FindBestMethod ( members . Cast < MethodBase > ( ) , args ) ;
1939
-
1940
- if ( applicableMethods . Length > 0 )
1941
- return applicableMethods ;
1942
- }
1943
-
1944
- return new MethodData [ 0 ] ;
1945
- }
1946
-
1947
- private MethodData FindInvokeMethod ( Type type )
1948
- {
1949
- var flags = BindingFlags . Public | BindingFlags . DeclaredOnly |
1950
- BindingFlags . Instance | _bindingCase ;
1951
- foreach ( var t in SelfAndBaseTypes ( type ) )
1952
- {
1953
- var method = t . FindMembers ( MemberTypes . Method , flags , _memberFilterCase , "Invoke" )
1954
- . Cast < MethodBase > ( )
1955
- . SingleOrDefault ( ) ;
1956
-
1957
- if ( method != null )
1958
- return MethodData . Gen ( method ) ;
1959
- }
1960
-
1961
- return null ;
1962
- }
1963
-
1964
- private MethodData [ ] FindExtensionMethods ( string methodName , Expression [ ] args )
1965
- {
1966
- var matchMethods = _arguments . GetExtensionMethods ( methodName ) ;
1967
-
1968
- return MethodResolution . FindBestMethod ( matchMethods , args ) ;
1969
- }
1970
-
1971
- private MethodData [ ] FindIndexer ( Type type , Expression [ ] args )
1972
- {
1973
- foreach ( var t in SelfAndBaseTypes ( type ) )
1974
- {
1975
- MemberInfo [ ] members = t . GetDefaultMembers ( ) ;
1976
- if ( members . Length != 0 )
1977
- {
1978
- IEnumerable < MethodData > methods = members .
1979
- OfType < PropertyInfo > ( ) .
1980
- Select ( p => ( MethodData ) new IndexerData ( p ) ) ;
1981
-
1982
- var applicableMethods = MethodResolution . FindBestMethod ( methods , args ) ;
1983
- if ( applicableMethods . Length > 0 )
1984
- return applicableMethods ;
1985
- }
1986
- }
1987
-
1988
- return new MethodData [ 0 ] ;
1989
- }
1990
-
1991
- private static IEnumerable < Type > SelfAndBaseTypes ( Type type )
1992
- {
1993
- if ( type . IsInterface )
1994
- {
1995
- var types = new List < Type > ( ) ;
1996
- AddInterface ( types , type ) ;
1997
-
1998
- types . Add ( typeof ( object ) ) ;
1999
-
2000
- return types ;
2001
- }
2002
- return SelfAndBaseClasses ( type ) ;
2003
- }
2004
-
2005
- private static IEnumerable < Type > SelfAndBaseClasses ( Type type )
2006
- {
2007
- while ( type != null )
2008
- {
2009
- yield return type ;
2010
- type = type . BaseType ;
2011
- }
2012
- }
2013
-
2014
- private static void AddInterface ( List < Type > types , Type type )
2015
- {
2016
- if ( ! types . Contains ( type ) )
2017
- {
2018
- types . Add ( type ) ;
2019
- foreach ( Type t in type . GetInterfaces ( ) )
2020
- {
2021
- AddInterface ( types , t ) ;
2022
- }
2023
- }
2024
- }
2025
-
2026
1915
private static bool IsWritable ( Expression expression )
2027
1916
{
2028
1917
switch ( expression . NodeType )
@@ -2042,7 +1931,6 @@ private static bool IsWritable(Expression expression)
2042
1931
}
2043
1932
case ExpressionType . Parameter :
2044
1933
return true ;
2045
-
2046
1934
}
2047
1935
2048
1936
return false ;
@@ -2211,7 +2099,7 @@ private MethodData FindBinaryOperator(string operatorName, Expression left, Expr
2211
2099
MethodData userDefinedOperator = null ;
2212
2100
2213
2101
// try to find the user defined operator on both operands
2214
- var opOnLeftType = FindMethods ( leftType , operatorName , true , args ) ;
2102
+ var opOnLeftType = _memberFinder . FindMethods ( leftType , operatorName , true , args ) ;
2215
2103
if ( opOnLeftType . Length > 1 )
2216
2104
throw error ;
2217
2105
@@ -2220,7 +2108,7 @@ private MethodData FindBinaryOperator(string operatorName, Expression left, Expr
2220
2108
2221
2109
if ( leftType != rightType )
2222
2110
{
2223
- var opOnRightType = FindMethods ( rightType , operatorName , true , args ) ;
2111
+ var opOnRightType = _memberFinder . FindMethods ( rightType , operatorName , true , args ) ;
2224
2112
if ( opOnRightType . Length > 1 )
2225
2113
throw error ;
2226
2114
0 commit comments