@@ -1267,6 +1267,7 @@ Expression ParseTypeAccess(Type type)
1267
1267
{
1268
1268
int errorPos = _textParser . CurrentToken . Pos ;
1269
1269
_textParser . NextToken ( ) ;
1270
+
1270
1271
if ( _textParser . CurrentToken . Id == TokenId . Question )
1271
1272
{
1272
1273
if ( ! type . GetTypeInfo ( ) . IsValueType || IsNullableType ( type ) )
@@ -1280,23 +1281,36 @@ Expression ParseTypeAccess(Type type)
1280
1281
bool shorthand = _textParser . CurrentToken . Id == TokenId . StringLiteral ;
1281
1282
if ( _textParser . CurrentToken . Id == TokenId . OpenParen || shorthand )
1282
1283
{
1283
- Expression [ ] args = shorthand
1284
- ? new [ ] { ParseStringLiteral ( ) }
1285
- : ParseArgumentList ( ) ;
1284
+ Expression [ ] args = shorthand ? new [ ] { ParseStringLiteral ( ) } : ParseArgumentList ( ) ;
1285
+
1286
+ // If only 1 argument, and if the type is a Nullable, just make Nullable
1287
+ if ( args . Length == 1 )
1288
+ {
1289
+ Type argType = args [ 0 ] . Type ;
1290
+
1291
+ if ( type . GetTypeInfo ( ) . IsValueType && IsNullableType ( type ) && argType . GetTypeInfo ( ) . IsValueType )
1292
+ {
1293
+ return Expression . Convert ( args [ 0 ] , type ) ;
1294
+ }
1295
+ }
1286
1296
1287
1297
MethodBase method ;
1288
1298
switch ( FindBestMethod ( type . GetConstructors ( ) , args , out method ) )
1289
1299
{
1290
1300
case 0 :
1291
1301
if ( args . Length == 1 )
1292
1302
return GenerateConversion ( args [ 0 ] , type , errorPos ) ;
1303
+
1293
1304
throw ParseError ( errorPos , Res . NoMatchingConstructor , GetTypeName ( type ) ) ;
1305
+
1294
1306
case 1 :
1295
1307
return Expression . New ( ( ConstructorInfo ) method , args ) ;
1308
+
1296
1309
default :
1297
1310
throw ParseError ( errorPos , Res . AmbiguousConstructorInvocation , GetTypeName ( type ) ) ;
1298
1311
}
1299
1312
}
1313
+
1300
1314
_textParser . ValidateToken ( TokenId . Dot , Res . DotOrOpenParenOrStringLiteralExpected ) ;
1301
1315
_textParser . NextToken ( ) ;
1302
1316
@@ -1311,12 +1325,10 @@ static Expression GenerateConversion(Expression expr, Type type, int errorPos)
1311
1325
1312
1326
if ( exprType . GetTypeInfo ( ) . IsValueType && type . GetTypeInfo ( ) . IsValueType )
1313
1327
{
1314
- if ( ( IsNullableType ( exprType ) || IsNullableType ( type ) ) &&
1315
- GetNonNullableType ( exprType ) == GetNonNullableType ( type ) )
1328
+ if ( ( IsNullableType ( exprType ) || IsNullableType ( type ) ) && GetNonNullableType ( exprType ) == GetNonNullableType ( type ) )
1316
1329
return Expression . Convert ( expr , type ) ;
1317
1330
1318
- if ( ( IsNumericType ( exprType ) || IsEnumType ( exprType ) ) &&
1319
- IsNumericType ( type ) || IsEnumType ( type ) )
1331
+ if ( ( IsNumericType ( exprType ) || IsEnumType ( exprType ) ) && IsNumericType ( type ) || IsEnumType ( type ) )
1320
1332
return Expression . ConvertChecked ( expr , type ) ;
1321
1333
}
1322
1334
0 commit comments