C # to format (indent, align) C #

We have a code generator that prints the schema of this database to automate our internal n-tier architecture. The result is several C # classes, one for each file.

In the code, in order to execute all the lines, we try to adhere to indentation and formatting as much as possible, but whenever you open a file in Visual Studio, formatting is a mistake. The quick ctrl-k, ctrl-d fixes it, but obviously this reformatting is lost the next time the class is created.

What I would like to know is there a way that I can somehow automatically format the contents of a text file in the same way as Visual Studio does?

Pseudo code

Create "code" object, passing text file to constructor Invoke "format" method Re-save text file 

Any help is greatly appreciated.

EDIT: I have to clarify - I want to be able to call formatting from my C # code, which creates a text file containing my generated C #. The code format can be standardized (not required for each developer), and I do not want to install any third-party applications.

It seems that I remember there a namespace containing many classes for creating C # in C #: http://msdn.microsoft.com/en-us/library/system.codedom(VS.80).aspx , but I'm not sure if it contains any classes that could help.

FURTHER EDITION: My code generator is a winforms application deployed through a one-time installation. It is used by many developers within the company. I need a solution that does not require each developer to have the tool installed on their machine.

+4
source share
6 answers

Expand answer to Cherian:

NArrange will allow you to do a lot of code formatting. It is open source and the source is available on their website, so you can potentially integrate and reinstall it using your tool.

You only need a DLL and just look at exe how to call formatting. It will also back up formatted code. This is a really good tool if it suits your needs.

+5
source

Take a look at Narrange . You probably need to automate these things as part of the assembly.
Not sure if it meets all your requirements. Quote:

NArrange is a .NET code decoder. which automatically organizes the code of members and elements inside .NET. classes.

+6
source

You can use CodeDOM and CSharpCodeProvider. This is all in the Microsoft.CSharp and System.CodeDom namespaces.

She is an example of a property:

 StringWriter writer = new StringWriter(); CSharpCodeProvider provider = new CSharpCodeProvider(); CodeMemberProperty property = new CodeMemberProperty(); property.Type = new CodeTypeReference(typeof(int)); property.Name = "MeaningOfLifeUniverseAndEverything"; property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(42))); provider.GenerateCodeFromMember(property, writer, null); Console.WriteLine(writer.GetStringBuilder().ToString()); 

This code will generate:

 private int MeaningOfLifeUniverseAndEverything { get { return 42; } } 

CodeDOM is a fairly common way to generate code. It’s good that you can create several languages. Perhaps you can find CodeProvider Erlang.NET?

You may be able to make multiple shortcuts using CodeSnippetExpression.

+1
source

To correctly retreat the code programmatically, you will need the Microsoft.CodeAnalysis.CSharp nuget package and the .NET framework 4.6+. Code example:

 public string ArrangeUsingRoslyn(string csCode) { var tree = CSharpSyntaxTree.ParseText(csCode); var root = tree.GetRoot().NormalizeWhitespace(); var ret = root.ToFullString(); return ret; } 

Single line:

 csCode = CSharpSyntaxTree.ParseText(csCode).GetRoot().NormalizeWhitespace().ToFullString(); 

You can also use NArrange to sort methods in your cs file, organize usage, create regions, etc. Note that NArrange does not indent.

+1
source

Only if you use the code generator as a complement to VS - each developer will have different settings.

0
source

Here's how to do it from a macro or add-in context:

 var dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.8.0"); dte.ExecuteCommand("File.OpenFile", filename); dte.ExecuteCommand("Edit.FormatDocument", filename); dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesYes); 

A warning. As @Greg Hurlman says, the output will depend on the current user settings.

Edit:

Unfortunately, your method requires that I have a VS instance running next to my winforms application. Can you come up with a way to create a VS instance from my application (if possible)?

I think it is possible to do this from your Win.Form application. However, you will have to have Visual Studio installed on the computer with the code.

Try the following:

 var dte = (EnvDTE80.DTE2)Microsoft.VisualBasic.Interaction.CreateObject("VisualStudio.DTE.8.0", ""); dte.ExecuteCommand("File.OpenFile", filename); dte.ExecuteCommand("Edit.FormatDocument", filename); dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesYes); 

Keep in mind that you will need references to the assembly of EnvDTE80.dll.

0
source

All Articles