Pregunta

¿Cómo puedo hacer que el CS genera a partir de un código como el siguiente formato es muy bien, es decir, como si presionamos Ctrl + K + D ? Es C #

Estamos haciendo algo en la línea de:

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("};"));
¿Fue útil?

Solución

CodeDom es realmente para la generación de código en tiempo de ejecución. Si usted está buscando para generar código en tiempo de diseño o en tiempo de compilación, se debe utilizar plantillas T4.

T4 permite dar formato a la salida código exactamente cómo desea que aparezca:

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

Otros consejos

En Visual Studio, vaya a

Herramienta -> Opciones de texto Editor-> C # -> Formato

Ctrl - K -. D utilizará la configuración hay que formatear el código

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top