@@ -13,14 +13,15 @@ namespace ExpressionDebugger
1313{
1414 public class ExpressionTranslator : ExpressionVisitor
1515 {
16- private const int Tabsize = 4 ;
16+ private const int _tabsize = 4 ;
1717 private StringWriter _writer ;
1818 private int _indentLevel ;
1919 private StringWriter _appendWriter ;
2020
2121 private Dictionary < Type , string > _typeNames ;
2222 private HashSet < string > _usings ;
2323 private Dictionary < object , string > _constants ;
24+ private Dictionary < Type , object > _defaults ;
2425
2526 public Dictionary < object , string > Constants => _constants ?? ( _constants = new Dictionary < object , string > ( ) ) ;
2627 public Dictionary < Type , string > TypeNames => _typeNames ?? ( _typeNames = new Dictionary < Type , string > ( ) ) ;
@@ -74,7 +75,7 @@ private static int GetPrecedence(ExpressionType nodeType)
7475 case ExpressionType . MultiplyAssign :
7576 case ExpressionType . MultiplyAssignChecked :
7677 case ExpressionType . OrAssign :
77- case ExpressionType . PowerAssign :
78+ // case ExpressionType.PowerAssign:
7879 case ExpressionType . Quote :
7980 case ExpressionType . RightShiftAssign :
8081 case ExpressionType . SubtractAssign :
@@ -156,9 +157,9 @@ private static int GetPrecedence(ExpressionType nodeType)
156157 case ExpressionType . UnaryPlus :
157158 return 13 ;
158159
159- // Power
160- case ExpressionType . Power :
161- return 14 ;
160+ //// Power
161+ // case ExpressionType.Power:
162+ // return 14;
162163
163164 default :
164165 return 100 ;
@@ -191,7 +192,7 @@ private static bool ShouldGroup(Expression node, ExpressionType parentNodeType,
191192 case ExpressionType . Divide :
192193 case ExpressionType . Modulo :
193194 case ExpressionType . LeftShift :
194- case ExpressionType . Power :
195+ // case ExpressionType.Power:
195196 case ExpressionType . RightShift :
196197 case ExpressionType . Subtract :
197198 case ExpressionType . SubtractChecked :
@@ -227,7 +228,7 @@ private void WriteLine()
227228 {
228229 _writer . WriteLine ( ) ;
229230
230- var spaceCount = _indentLevel * Tabsize ;
231+ var spaceCount = _indentLevel * _tabsize ;
231232 _writer . Write ( new string ( ' ' , spaceCount ) ) ;
232233 }
233234
@@ -423,11 +424,6 @@ private string Translate(Type type)
423424 return "ushort" ;
424425 if ( type == typeof ( void ) )
425426 return "void" ;
426- if ( type . GetTypeInfo ( ) . IsNotPublic )
427- {
428- HasDynamic = true ;
429- return "dynamic" ;
430- }
431427#if ! NETSTANDARD1_3
432428 if ( typeof ( IDynamicMetaObjectProvider ) . GetTypeInfo ( ) . IsAssignableFrom ( type . GetTypeInfo ( ) ) )
433429 {
@@ -791,6 +787,22 @@ private void WriteValue(object value)
791787 }
792788 else
793789 {
790+ var type = value . GetType ( ) ;
791+ if ( type . GetTypeInfo ( ) . IsValueType )
792+ {
793+ if ( _defaults == null )
794+ _defaults = new Dictionary < Type , object > ( ) ;
795+ if ( ! _defaults . TryGetValue ( type , out var def ) )
796+ {
797+ def = Activator . CreateInstance ( type ) ;
798+ _defaults [ type ] = def ;
799+ }
800+ if ( value . Equals ( def ) )
801+ {
802+ Write ( $ "default({ Translate ( type ) } )") ;
803+ return ;
804+ }
805+ }
794806 Write ( GetConstant ( value ) ) ;
795807 }
796808 }
@@ -1792,7 +1804,7 @@ public override string ToString()
17921804 }
17931805
17941806 WriteModifier ( true ) ;
1795- Write ( "class " , Definitions . TypeName ) ;
1807+ Write ( "partial class " , Definitions . TypeName ) ;
17961808 if ( implements ? . Any ( ) == true )
17971809 {
17981810 Write ( " : " , string . Join ( ", " , implements ) ) ;
@@ -1803,7 +1815,7 @@ public override string ToString()
18031815 {
18041816 foreach ( var constant in constants )
18051817 {
1806- WriteModifier ( true ) ;
1818+ WriteModifier ( false ) ;
18071819 Write ( constant ) ;
18081820 }
18091821 WriteLine ( ) ;
0 commit comments