@@ -21,38 +21,41 @@ public class ExpressionTranslator : ExpressionVisitor
2121 private Dictionary < Type , string > _typeNames ;
2222 private HashSet < string > _usings ;
2323 private Dictionary < object , string > _constants ;
24- private readonly ExpressionDefinitions _definitions ;
2524
2625 public Dictionary < object , string > Constants => _constants ?? ( _constants = new Dictionary < object , string > ( ) ) ;
2726 public Dictionary < Type , string > TypeNames => _typeNames ?? ( _typeNames = new Dictionary < Type , string > ( ) ) ;
2827
2928 public bool HasDynamic { get ; private set ; }
29+ public ExpressionDefinitions Definitions { get ; }
30+ public Expression Expression { get ; private set ; }
3031
31- public ExpressionTranslator ( ExpressionDefinitions definitions = null )
32+ private ExpressionTranslator ( ExpressionDefinitions definitions = null )
3233 {
33- _definitions = definitions ;
34+ Definitions = definitions ;
3435 _writer = new StringWriter ( ) ;
3536 ResetIndentLevel ( ) ;
3637 }
3738
3839 private void ResetIndentLevel ( )
3940 {
4041 _indentLevel = 0 ;
41- if ( _definitions ? . TypeName != null )
42+ if ( Definitions ? . TypeName != null )
4243 {
4344 _indentLevel ++ ;
44- if ( _definitions . Namespace != null )
45+ if ( Definitions . Namespace != null )
4546 _indentLevel ++ ;
4647 }
4748 }
4849
49- public string Translate ( Expression node )
50+ public static ExpressionTranslator Create ( Expression node , ExpressionDefinitions definitions = null )
5051 {
52+ var translator = new ExpressionTranslator ( definitions ) ;
5153 if ( node . NodeType == ExpressionType . Lambda )
52- VisitLambda ( ( LambdaExpression ) node , LambdaType . Main ) ;
54+ translator . VisitLambda ( ( LambdaExpression ) node , LambdaType . Main ) ;
5355 else
54- Visit ( node ) ;
55- return this . ToString ( ) ;
56+ translator . Visit ( node ) ;
57+ translator . Expression = node ;
58+ return translator ;
5659 }
5760
5861 private static int GetPrecedence ( ExpressionType nodeType )
@@ -574,7 +577,7 @@ private IEnumerable<Expression> VisitBlockBody(IList<Expression> exprs, bool sho
574577 Expression next ;
575578 if ( isInline )
576579 {
577- if ( shouldReturn && i == last )
580+ if ( shouldReturn && i == last && expr . NodeType != ExpressionType . Throw )
578581 Write ( "return " ) ;
579582 next = Visit ( expr ) ;
580583 Write ( ";" ) ;
@@ -1093,7 +1096,7 @@ private ParameterExpression VisitParameterDeclaration(ParameterExpression node)
10931096 private void WriteModifier ( bool isPublic )
10941097 {
10951098 WriteNextLine ( isPublic ? "public " : "private " ) ;
1096- if ( _definitions ? . IsStatic == true )
1099+ if ( Definitions ? . IsStatic == true )
10971100 Write ( "static " ) ;
10981101 }
10991102
@@ -1102,7 +1105,7 @@ private Expression VisitLambda(LambdaExpression node, LambdaType type)
11021105 if ( type == LambdaType . Function || type == LambdaType . Main )
11031106 {
11041107 var name = type == LambdaType . Main
1105- ? ( _definitions ? . MethodName ?? "Main" )
1108+ ? ( Definitions ? . MethodName ?? "Main" )
11061109 : GetName ( node ) ;
11071110 WriteModifier ( type == LambdaType . Main ) ;
11081111 Write ( Translate ( node . ReturnType ) , " " , name ) ;
@@ -1325,6 +1328,7 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
13251328 {
13261329 obj = VisitGroup ( node . Object , node . NodeType ) ;
13271330 }
1331+ #if ! NET40
13281332 else if ( ! node . Method . IsPublic || node . Method . DeclaringType ? . GetTypeInfo ( ) . IsNotPublic == true)
13291333 {
13301334 isNotPublic = true ;
@@ -1339,6 +1343,7 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
13391343 var func = node . Method . CreateDelegate ( del ) ;
13401344 Write ( GetConstant ( func , GetVarName ( node . Method . Name ) ) , ".Invoke" ) ;
13411345 }
1346+ #endif
13421347 else if ( node . Method . GetCustomAttribute < ExtensionAttribute > ( ) != null )
13431348 {
13441349 isExtension = true ;
@@ -1749,14 +1754,14 @@ public override string ToString()
17491754 _writer = codeWriter ;
17501755
17511756 //exercise to update _usings
1752- var implements = _definitions ? . Implements ? . OrderBy ( it => it . GetTypeInfo ( ) . IsInterface ? 1 : 0 )
1757+ var implements = Definitions ? . Implements ? . OrderBy ( it => it . GetTypeInfo ( ) . IsInterface ? 1 : 0 )
17531758 . Select ( Translate )
17541759 . ToList ( ) ;
17551760 var constants = _constants ? . OrderBy ( it => it . Value )
17561761 . Select ( kvp => $ "{ Translate ( kvp . Key . GetType ( ) ) } { kvp . Value } ;")
17571762 . ToList ( ) ;
17581763
1759- if ( _definitions ? . TypeName != null )
1764+ if ( Definitions ? . TypeName != null )
17601765 {
17611766 if ( _usings != null )
17621767 {
@@ -1780,14 +1785,14 @@ public override string ToString()
17801785 }
17811786 WriteLine ( ) ;
17821787 }
1783- if ( _definitions ? . Namespace != null )
1788+ if ( Definitions ? . Namespace != null )
17841789 {
1785- WriteNextLine ( "namespace " , _definitions . Namespace ) ;
1790+ WriteNextLine ( "namespace " , Definitions . Namespace ) ;
17861791 Indent ( ) ;
17871792 }
17881793
17891794 WriteModifier ( true ) ;
1790- Write ( "class " , _definitions . TypeName ) ;
1795+ Write ( "class " , Definitions . TypeName ) ;
17911796 if ( implements ? . Any ( ) == true )
17921797 {
17931798 Write ( " : " , string . Join ( ", " , implements ) ) ;
@@ -1806,10 +1811,10 @@ public override string ToString()
18061811 _writer . Write ( temp ) ;
18071812 if ( _appendWriter != null )
18081813 _writer . Write ( _appendWriter ) ;
1809- if ( _definitions ? . TypeName != null )
1814+ if ( Definitions ? . TypeName != null )
18101815 {
18111816 Outdent ( ) ;
1812- if ( _definitions ? . Namespace != null )
1817+ if ( Definitions ? . Namespace != null )
18131818 Outdent ( ) ;
18141819 }
18151820 return _writer . ToString ( ) ;
0 commit comments