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

前端 未结 6 1812
心在旅途
心在旅途 2021-01-02 16:17

We have a code generator that munges the schema of a given database to automate our inhouse n-tier architecture. The output is various C# partial classes, one per file.

相关标签:
6条回答
  • 2021-01-02 16:42

    Only if you're running the code generator as a VS add-on - each developer is going to have different settings.

    0 讨论(0)
  • 2021-01-02 16:47

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

    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);
    

    Warning: As @Greg Hurlman says, the output will vary depending on the user's current options.

    Edit:

    unfortunately your method requires me to have an instance of VS running alongside my winforms app. Can you think of a way to create an instance of VS from within my app (if that's even possible)?

    I think it might be possible to do from within your Win.Form app. However, you'll have to have Visual Studio installed on the machine running the code.

    Try this:

    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'll need references to the EnvDTE80.dll assembly.

    0 讨论(0)
  • 2021-01-02 16:52

    To Expand on Cherian's answer:

    NArrange will allow you to do a lot of code formatting. It is open source, and the source is available on their site, so you could potentially integrate and redistbute it with your tool.

    You should only need the dll's and just look at the exe on how to call the formatting. It will also create a backup of the code being formatted. It is really a nice tool if it fits your needs.

    0 讨论(0)
  • 2021-01-02 16:54

    To properly indent code programmatically you would need Microsoft.CodeAnalysis.CSharp nuget package and .NET framework 4.6+. Sample code:

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

    One-liner:

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

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

    0 讨论(0)
  • 2021-01-02 16:56

    Take a look at Narrange.You'll probably need to automate these things as part of the build.
    Not sure if it meets all your requirements though.
    To quote:

    NArrange is a .NET code beautifier that automatically organizes code members and elements within .NET classes.

    0 讨论(0)
  • 2021-01-02 16:59

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

    Her 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;
        }
    }
    

    The CodeDOM is a quite chatty way to generate code. The good thing is that you can generate multiple languages. Perhaps you can find a Erlang.NET CodeProvider?

    You might be able to do a few shortcuts by using CodeSnippetExpression.

    0 讨论(0)
提交回复
热议问题