Program format with CodeDom code

How can I make sure that the CS generated from code like the following is perfectly formatted, i.e. as if we pressed CTRL+ K+ D? It with#

We are doing something like:

CodeMemberMethod membMethod = new CodeMemberMethod();
membMethod.Attributes = MemberAttributes.Static | MemberAttributes.Public;
membMethod.ReturnType = new CodeTypeReference("IEnumerable<" + TableNameAsSinglular + ">");
membMethod.Name = "Get" + TableName;
membMethod.Statements.Add(new CodeSnippetStatement(DataBaseContext + " dcontext = new " + DataBaseContext + "(ConnectionString);"));
membMethod.Statements.Add(new CodeSnippetStatement("var records = from record in dcontext." + TableName + " select new " + TableNameAsSinglular + "{"));
    int iCount = 0;

    //Add columns fields
    foreach (DataRow dr in sqlTable.Rows)
    {
        if (iCount == 4)
        break;
        string strColName = dr["ColumnName"].ToString().Replace(" ", "");
        membMethod.Statements.Add(new CodeSnippetStatement(strColName + "=" + "record." + strColName + ","));
        iCount++;
    }

membMethod.Statements.Add(new CodeSnippetStatement("};"));
+5
source share
2 answers

CodeDom is really designed to generate runtime code. If you want to generate code at design time or compilation time, you should use T4 templates.

T4 allows you to format the code output exactly the way you want:

http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx

+2
source

Visual Studio

โ†’ Opetions-Text Editor- > # โ†’

Ctrl - K - D .

0

All Articles