@@ -5626,9 +5626,12 @@ public static string ToExpressionString(this Expression expr,
56265626 lts = new List < LabelTarget > ( ) ;
56275627 sb = expr . CreateExpressionString ( sb , paramsExprs , uniqueExprs , lts , 2 , stripNamespace , printType , identSpaces , tryPrintConstant ) . Append ( ';' ) ;
56285628
5629- sb . Insert ( 0 , $ "var l = new LabelTarget[{ lts . Count } ]; // the labels { NewLine } ") ;
5630- sb . Insert ( 0 , $ "var e = new Expression[{ uniqueExprs . Count } ]; // the unique expressions { NewLine } ") ;
5631- sb . Insert ( 0 , $ "var p = new ParameterExpression[{ paramsExprs . Count } ]; // the parameter expressions { NewLine } ") ;
5629+ if ( lts . Count > 0 )
5630+ sb . Insert ( 0 , $ "var l = new LabelTarget[{ lts . Count } ]; // the labels { NewLine } ") ;
5631+ if ( uniqueExprs . Count > 0 )
5632+ sb . Insert ( 0 , $ "var e = new Expression[{ uniqueExprs . Count } ]; // the unique expressions { NewLine } ") ;
5633+ if ( paramsExprs . Count > 0 )
5634+ sb . Insert ( 0 , $ "var p = new ParameterExpression[{ paramsExprs . Count } ]; // the parameter expressions { NewLine } ") ;
56325635
56335636 return sb . ToString ( ) ;
56345637 }
@@ -5818,21 +5821,9 @@ internal static StringBuilder CreateExpressionString(this Expression e, StringBu
58185821 sb . AppendTypeof ( t , stripNamespace , printType ) ;
58195822 else
58205823 {
5821- // For the closure bound constant let's output `null` or default value with the comment for user to provide the actual value
5822- if ( ExpressionCompiler . IsClosureBoundConstant ( x . Value , x . Type ) )
5823- {
5824- if ( x . Type . IsValueType )
5825- sb . Append ( "default(" ) . Append ( x . Type . ToCode ( stripNamespace , printType ) ) . Append ( ')' ) ;
5826- else // specifying the type for the Constant, otherwise we will lost it with the `Constant(default(MyClass))` which is equivalent to `Constant(null)`
5827- sb . Append ( "null, " ) . AppendTypeof ( x . Type , stripNamespace , printType ) ;
5828- sb . NewLineIdent ( lineIdent ) . Append ( "// !!! Please provide the non-default value" ) . NewLineIdent ( lineIdent ) ;
5829- }
5830- else
5831- {
5832- sb . Append ( x . Value . ToCode ( CodePrinter . DefaultConstantValueToCode , stripNamespace , printType ) ) ;
5833- if ( x . Value . GetType ( ) != x . Type )
5834- sb . Append ( ", " ) . AppendTypeof ( x . Type , stripNamespace , printType ) ;
5835- }
5824+ sb . Append ( x . Value . ToCode ( CodePrinter . DefaultConstantValueToCode , stripNamespace , printType ) ) ;
5825+ if ( x . Value . GetType ( ) != x . Type )
5826+ sb . Append ( ", " ) . AppendTypeof ( x . Type , stripNamespace , printType ) ;
58365827 }
58375828 return sb . Append ( ')' ) ;
58385829 }
@@ -6121,6 +6112,15 @@ internal static StringBuilder CreateExpressionString(this Expression e, StringBu
61216112 sb . Append ( "MakeBinary(" ) . Append ( typeof ( ExpressionType ) . Name ) . Append ( '.' ) . Append ( name ) . Append ( ',' ) ;
61226113 sb . NewLineIdentExpr ( b . Left , paramsExprs , uniqueExprs , lts , lineIdent , stripNamespace , printType , identSpaces , tryPrintConstant ) . Append ( ',' ) ;
61236114 sb . NewLineIdentExpr ( b . Right , paramsExprs , uniqueExprs , lts , lineIdent , stripNamespace , printType , identSpaces , tryPrintConstant ) ;
6115+ if ( b . IsLiftedToNull || b . Method != null || b . Conversion != null )
6116+ {
6117+ sb . Append ( ',' ) . NewLineIdent ( lineIdent ) . Append ( "liftToNull: " ) . Append ( b . IsLiftedToNull . ToCode ( ) ) . Append ( ',' ) ;
6118+ sb . NewLineIdent ( lineIdent ) . AppendMethod ( b . Method , stripNamespace , printType ) ;
6119+ }
6120+ if ( b . Conversion != null )
6121+ {
6122+ sb . Append ( ',' ) . NewLineIdentExpr ( b . Conversion , paramsExprs , uniqueExprs , lts , lineIdent , stripNamespace , printType , identSpaces , tryPrintConstant ) ;
6123+ }
61246124 }
61256125 return sb . Append ( ')' ) ;
61266126 }
@@ -7074,6 +7074,12 @@ public static string ToCode(this Type type,
70747074 return ! printGenericTypeArgs ? string . Empty
70757075 : ( printType ? . Invoke ( type , type . Name ) ?? type . Name ) ;
70767076
7077+ if ( Nullable . GetUnderlyingType ( type ) is Type nullableElementType && ! type . IsGenericTypeDefinition )
7078+ {
7079+ var result = nullableElementType . ToCode ( stripNamespace , printType , printGenericTypeArgs ) + "?" ;
7080+ return printType ? . Invoke ( type , result ) ?? result ;
7081+ }
7082+
70777083 Type arrayType = null ;
70787084 if ( type . IsArray )
70797085 {
@@ -7246,7 +7252,7 @@ public interface IObjectToCode
72467252 private class ConstantValueToCode : CodePrinter . IObjectToCode
72477253 {
72487254 public string ToCode ( object x , bool stripNamespace = false , Func < Type , string , string > printType = null ) =>
7249- "default(" + x . GetType ( ) . ToCode ( stripNamespace , printType ) + ")" ;
7255+ "default(" + x . GetType ( ) . ToCode ( stripNamespace , printType ) + ") /* " + x . ToString ( ) + " !!! Please provide the non-default value */ ";
72507256 }
72517257
72527258
@@ -7289,6 +7295,12 @@ public static string ToCode(this object x, IObjectToCode notRecognizedToCode,
72897295 if ( x is bool b )
72907296 return b . ToCode ( ) ;
72917297
7298+ if ( x is int i )
7299+ return i . ToString ( ) ;
7300+
7301+ if ( x is double d )
7302+ return d . ToString ( ) ;
7303+
72927304 if ( x is string s )
72937305 return s . ToCode ( ) ;
72947306
@@ -7298,6 +7310,15 @@ public static string ToCode(this object x, IObjectToCode notRecognizedToCode,
72987310 if ( x is Type t )
72997311 return t . ToCode ( stripNamespace , printType ) ;
73007312
7313+ if ( x is Guid guid )
7314+ return "Guid.Parse(" + guid . ToString ( ) . ToCode ( ) + ")" ;
7315+
7316+ if ( x is DateTime date )
7317+ return "DateTime.Parse(" + date . ToString ( ) . ToCode ( ) + ")" ;
7318+
7319+ if ( x is TimeSpan time )
7320+ return "TimeSpan.Parse(" + time . ToString ( ) . ToCode ( ) + ")" ;
7321+
73017322 var xType = x . GetType ( ) ;
73027323 var xTypeInfo = xType . GetTypeInfo ( ) ;
73037324
0 commit comments