Is there way to format code produced by ExpressionToCode? In particular to add line-breaks and indentation as following:
var code = ExpressionToCode.ToCode(() => new A(new B(), new C()));
and the result code is:
() =>
new A(
new B(),
new C())
Not really. You could take the Roslyn
SyntaxTree
(usingToSyntaxTree
instead ofToCode
) and use that to format the output any way you want.It's actually quite easy to handle code formatting with Roslyn - you just have to inherit your own class from
SyntaxRewriter
. See http://www.christophdebaene.com/blog/2011/10/26/roslyn-formatting-code/ for more information.The problem of code formatting is actually quite complex, it needs quite a bit of thought.