3

Duplicate

C# to format (indent, align) C# properly


I am generating a bunch of code and would like it's initial output (pre ctrl+K, Ctrl+D) to look reasonable. Managing indents is a royal pain and there has to be a utility or class out there I can embed inside of my code generator. I am embedding this into a VS add-in and in a build process so I am not interested in an external program. I need something I can call programatically.

Recommendations?

1 Answer 1

2

Use the IndentedTextWriter that is provided with the Microsoft .NET Framework to help with indenting code. There is sample usage included with the class documentation.

Sign up to request clarification or add additional context in comments.

2 Comments

That's not going to be terribly useful without writing a parser to understand C# syntax and therefore decide when to indent/unindent.
Right, I misunderstood the question.