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

前端 未结 6 1813
心在旅途
心在旅途 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: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.

提交回复
热议问题