@@ -904,10 +904,9 @@ Expression ParseIdentifier()
904
904
}
905
905
else
906
906
{
907
- var lambda = expr as LambdaExpression ;
908
- if ( lambda != null )
907
+ if ( expr is LambdaExpression lambdaExpression )
909
908
{
910
- return ParseLambdaInvocation ( lambda ) ;
909
+ return ParseLambdaInvocation ( lambdaExpression ) ;
911
910
}
912
911
}
913
912
@@ -1021,14 +1020,25 @@ Expression ParseFunctionIs()
1021
1020
1022
1021
Expression [ ] args = ParseArgumentList ( ) ;
1023
1022
1024
- if ( args . Length != 1 )
1023
+ if ( args . Length != 1 && args . Length != 2 )
1025
1024
{
1026
- throw ParseError ( errorPos , Res . FunctionRequiresOneArg , functionName ) ;
1025
+ throw ParseError ( errorPos , Res . FunctionRequiresOneOrTwoArgs , functionName ) ;
1027
1026
}
1028
1027
1029
- Type resolvedType = ResolveTypeFromArgumentExpression ( functionName , args [ 0 ] ) ;
1028
+ Expression typeArgument ;
1029
+ Expression it ;
1030
+ if ( args . Length == 1 )
1031
+ {
1032
+ typeArgument = args [ 0 ] ;
1033
+ it = _it ;
1034
+ }
1035
+ else
1036
+ {
1037
+ typeArgument = args [ 1 ] ;
1038
+ it = args [ 0 ] ;
1039
+ }
1030
1040
1031
- return Expression . TypeIs ( _it , resolvedType ) ;
1041
+ return Expression . TypeIs ( it , ResolveTypeFromArgumentExpression ( functionName , typeArgument , args . Length ) ) ;
1032
1042
}
1033
1043
1034
1044
// As(...) function
@@ -1040,14 +1050,25 @@ Expression ParseFunctionAs()
1040
1050
1041
1051
Expression [ ] args = ParseArgumentList ( ) ;
1042
1052
1043
- if ( args . Length != 1 )
1053
+ if ( args . Length != 1 && args . Length != 2 )
1044
1054
{
1045
- throw ParseError ( errorPos , Res . FunctionRequiresOneArg , functionName ) ;
1055
+ throw ParseError ( errorPos , Res . FunctionRequiresOneOrTwoArgs , functionName ) ;
1046
1056
}
1047
1057
1048
- Type resolvedType = ResolveTypeFromArgumentExpression ( functionName , args [ 0 ] ) ;
1058
+ Expression typeArgument ;
1059
+ Expression it ;
1060
+ if ( args . Length == 1 )
1061
+ {
1062
+ typeArgument = args [ 0 ] ;
1063
+ it = _it ;
1064
+ }
1065
+ else
1066
+ {
1067
+ typeArgument = args [ 1 ] ;
1068
+ it = args [ 0 ] ;
1069
+ }
1049
1070
1050
- return Expression . TypeAs ( _it , resolvedType ) ;
1071
+ return Expression . TypeAs ( it , ResolveTypeFromArgumentExpression ( functionName , typeArgument , args . Length ) ) ;
1051
1072
}
1052
1073
1053
1074
// Cast(...) function
@@ -1059,14 +1080,25 @@ Expression ParseFunctionCast()
1059
1080
1060
1081
Expression [ ] args = ParseArgumentList ( ) ;
1061
1082
1062
- if ( args . Length != 1 )
1083
+ if ( args . Length != 1 && args . Length != 2 )
1063
1084
{
1064
- throw ParseError ( errorPos , Res . FunctionRequiresOneArg , functionName ) ;
1085
+ throw ParseError ( errorPos , Res . FunctionRequiresOneOrTwoArgs , functionName ) ;
1065
1086
}
1066
1087
1067
- Type resolvedType = ResolveTypeFromArgumentExpression ( functionName , args [ 0 ] ) ;
1088
+ Expression typeArgument ;
1089
+ Expression it ;
1090
+ if ( args . Length == 1 )
1091
+ {
1092
+ typeArgument = args [ 0 ] ;
1093
+ it = _it ;
1094
+ }
1095
+ else
1096
+ {
1097
+ typeArgument = args [ 1 ] ;
1098
+ it = args [ 0 ] ;
1099
+ }
1068
1100
1069
- return Expression . ConvertChecked ( _it , resolvedType ) ;
1101
+ return Expression . ConvertChecked ( it , ResolveTypeFromArgumentExpression ( functionName , typeArgument , args . Length ) ) ;
1070
1102
}
1071
1103
1072
1104
Expression GenerateConditional ( Expression test , Expression expressionIfTrue , Expression expressionIfFalse , bool nullPropagating , int errorPos )
@@ -1101,8 +1133,7 @@ Expression GenerateConditional(Expression test, Expression expressionIfTrue, Exp
1101
1133
{
1102
1134
// - create nullable constant from expressionIfTrue with type from expressionIfFalse
1103
1135
// - convert expressionIfFalse to nullable (unless it's already nullable)
1104
-
1105
- Type nullableType = TypeHelper . ToNullableType ( expressionIfFalse . Type ) ;
1136
+ var nullableType = TypeHelper . ToNullableType ( expressionIfFalse . Type ) ;
1106
1137
expressionIfTrue = Expression . Constant ( null , nullableType ) ;
1107
1138
1108
1139
if ( ! TypeHelper . IsNullableType ( expressionIfFalse . Type ) )
@@ -1343,10 +1374,10 @@ private Expression CreateNewExpression(List<DynamicProperty> properties, List<Ex
1343
1374
#endif
1344
1375
}
1345
1376
1346
- IEnumerable < PropertyInfo > propertyInfos = type . GetProperties ( ) ;
1377
+ var propertyInfos = type . GetProperties ( ) ;
1347
1378
if ( type . GetTypeInfo ( ) . BaseType == typeof ( DynamicClass ) )
1348
1379
{
1349
- propertyInfos = propertyInfos . Where ( x => x . Name != "Item" ) ;
1380
+ propertyInfos = propertyInfos . Where ( x => x . Name != "Item" ) . ToArray ( ) ;
1350
1381
}
1351
1382
1352
1383
Type [ ] propertyTypes = propertyInfos . Select ( p => p . PropertyType ) . ToArray ( ) ;
@@ -1836,8 +1867,9 @@ Expression ParseEnumerable(Expression instance, Type elementType, string methodN
1836
1867
return Expression . Call ( callType , methodName , typeArgs , args ) ;
1837
1868
}
1838
1869
1839
- private Type ResolveTypeFromArgumentExpression ( string functionName , Expression argumentExpression )
1870
+ private Type ResolveTypeFromArgumentExpression ( string functionName , Expression argumentExpression , int ? arguments = null )
1840
1871
{
1872
+ string argument = arguments == null ? string . Empty : arguments == 1 ? "first " : "second " ;
1841
1873
switch ( argumentExpression )
1842
1874
{
1843
1875
case ConstantExpression constantExpression :
@@ -1850,21 +1882,16 @@ private Type ResolveTypeFromArgumentExpression(string functionName, Expression a
1850
1882
return type ;
1851
1883
1852
1884
default :
1853
- throw ParseError ( _textParser . CurrentToken . Pos , Res . FunctionRequiresOneNotNullArgOfType , functionName , "string or System.Type" ) ;
1885
+ throw ParseError ( _textParser . CurrentToken . Pos , Res . FunctionRequiresNotNullArgOfType , functionName , argument , "string or System.Type" ) ;
1854
1886
}
1855
1887
1856
1888
default :
1857
- throw ParseError ( _textParser . CurrentToken . Pos , Res . FunctionRequiresOneNotNullArgOfType , functionName , "ConstantExpression" ) ;
1889
+ throw ParseError ( _textParser . CurrentToken . Pos , Res . FunctionRequiresNotNullArgOfType , functionName , argument , "ConstantExpression" ) ;
1858
1890
}
1859
1891
}
1860
1892
1861
1893
private Type ResolveTypeStringFromArgument ( string functionName , string typeName )
1862
1894
{
1863
- if ( string . IsNullOrEmpty ( typeName ) )
1864
- {
1865
- throw ParseError ( _textParser . CurrentToken . Pos , Res . FunctionRequiresOneNotNullArg , functionName , typeName ) ;
1866
- }
1867
-
1868
1895
Type resultType = _typeFinder . FindTypeByName ( typeName , new [ ] { _it , _parent , _root } , true ) ;
1869
1896
if ( resultType == null )
1870
1897
{
@@ -1962,7 +1989,7 @@ static bool TryGetMemberName(Expression expression, out string memberName)
1962
1989
var memberExpression = expression as MemberExpression ;
1963
1990
if ( memberExpression == null && expression . NodeType == ExpressionType . Coalesce )
1964
1991
{
1965
- memberExpression = ( expression as BinaryExpression ) . Left as MemberExpression ;
1992
+ memberExpression = ( expression as BinaryExpression ) ? . Left as MemberExpression ;
1966
1993
}
1967
1994
1968
1995
if ( memberExpression != null )
0 commit comments