@@ -6760,7 +6760,7 @@ internal static StringBuilder ToCSharpString(this Expression e, StringBuilder sb
6760
6760
{
6761
6761
var x = ( ConstantExpression ) e ;
6762
6762
if ( x . Value == null )
6763
- return x . Type != null
6763
+ return x . Type != null && x . Type . IsValueType
6764
6764
? sb . Append ( "default(" ) . Append ( x . Type . ToCode ( stripNamespace , printType ) ) . Append ( ')' )
6765
6765
: sb . Append ( "null" ) ;
6766
6766
@@ -6948,7 +6948,8 @@ internal static StringBuilder ToCSharpString(this Expression e, StringBuilder sb
6948
6948
sb . NewLineIdentCs ( body , EnclosedIn . LambdaBody , lineIdent + identSpaces , stripNamespace , printType , identSpaces , notRecognizedToCode ) ;
6949
6949
else
6950
6950
{
6951
- sb . NewLine ( lineIdent , identSpaces ) . Append ( '{' ) ;
6951
+ sb . NewLineIdent ( lineIdent ) . Append ( '{' ) ;
6952
+
6952
6953
// Body handles `;` itself
6953
6954
if ( body is BlockExpression bb )
6954
6955
bb . BlockToCSharpString ( sb , lineIdent + identSpaces , stripNamespace , printType , identSpaces , notRecognizedToCode ,
@@ -6959,7 +6960,7 @@ internal static StringBuilder ToCSharpString(this Expression e, StringBuilder sb
6959
6960
if ( isBodyExpression )
6960
6961
sb . AddSemicolonIfFits ( ) ;
6961
6962
}
6962
- sb . NewLine ( lineIdent , identSpaces ) . Append ( '}' ) ;
6963
+ sb . NewLineIdent ( lineIdent ) . Append ( '}' ) ;
6963
6964
}
6964
6965
return sb . Append ( ')' ) ;
6965
6966
}
@@ -7011,7 +7012,14 @@ internal static StringBuilder ToCSharpString(this Expression e, StringBuilder sb
7011
7012
}
7012
7013
case ExpressionType . Block :
7013
7014
{
7014
- return BlockToCSharpString ( ( BlockExpression ) e , sb , lineIdent , stripNamespace , printType , identSpaces , notRecognizedToCode : notRecognizedToCode ) ;
7015
+ if ( enclosedIn == EnclosedIn . Block )
7016
+ return BlockToCSharpString ( ( BlockExpression ) e , sb , lineIdent , stripNamespace , printType , identSpaces , notRecognizedToCode : notRecognizedToCode ) ;
7017
+ else
7018
+ {
7019
+ sb . Append ( '{' ) ;
7020
+ BlockToCSharpString ( ( BlockExpression ) e , sb , lineIdent + identSpaces , stripNamespace , printType , identSpaces , notRecognizedToCode : notRecognizedToCode ) ;
7021
+ return sb . AddSemicolonIfFits ( ) . NewLineIdent ( lineIdent ) . Append ( '}' ) ;
7022
+ }
7015
7023
}
7016
7024
case ExpressionType . Loop :
7017
7025
{
@@ -7162,11 +7170,12 @@ void PrintPart(Expression part)
7162
7170
}
7163
7171
case ExpressionType . Default :
7164
7172
{
7165
- return e . Type == typeof ( void ) ? sb // `default(void)` does not make sense in the C#
7166
- : sb . Append ( "default(" ) . Append ( e . Type . ToCode ( stripNamespace , printType ) ) . Append ( ')' ) ;
7173
+ return e . Type == typeof ( void ) ? sb : // `default(void)` does not make sense in the C#
7174
+ ! e . Type . IsValueType && ! e . Type . IsGenericParameter ? sb . Append ( "null" ) :
7175
+ sb . Append ( "default(" ) . Append ( e . Type . ToCode ( stripNamespace , printType ) ) . Append ( ')' ) ;
7167
7176
}
7168
7177
case ExpressionType . TypeIs :
7169
- case ExpressionType . TypeEqual :
7178
+ case ExpressionType . TypeEqual : // TODO: type equal
7170
7179
{
7171
7180
var x = ( TypeBinaryExpression ) e ;
7172
7181
sb . Append ( '(' ) ;
@@ -8118,3 +8127,4 @@ public RequiresUnreferencedCodeAttribute(string message)
8118
8127
}
8119
8128
}
8120
8129
#endif
8130
+ }
0 commit comments