2010-02-18 17 views
11

Sto utilizzando CodeCompileUnit e CSharpCodeProvider per generare un codice sorgente. Aggiunge l'intestazione sottostante a tutto il codice generato. C'è un modo per personalizzare il commento così dice qualcos'altro?Come si personalizza il commento generato automaticamente quando si utilizza la generazione del codice CodeDom .NET?

// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:2.0.50727.3053 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 

risposta

0

Anche se questo non sembra essere direttamente supportata da CodeDOM, è possibile utilizzare il fatto che questo commento è esplicitamente delimitato dalle tag <auto-generated> e </auto-generated>. Così si potrebbe alterare questo commento semplicemente effettuando operazioni di stringa sull'uscita di CodeDOM:

var provider = new CSharpCodeProvider(); 
string generatedCode; 
using (var output = new StringWriter()) 
{ 
    provider.GenerateCodeFrom…(…, output, …); 
    generatedCode = output.ToString(); 
} 
string modifiedCode = Regex.Replace(generatedCode, …); // modify the output as you see fit 
3

Si può semplicemente aggiungere i tuoi commenti all'inizio del file da assomigliare a questo:

//---------------------------------------------------------------------------- 
// My comments 
// Are go here 
//---------------------------------------------------------------------------- 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:2.0.50727.3053 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//---------------------------------------------------------------------------- 

Appena prima di generare il CompileUnit ad un TextWriter fare:

CSharpCodeProvider provider = new CSharpCodeProvider(); 
var tw = new IndentedTextWriter(new StreamWriter(filename, false), " "); 

tw.WriteLine("//----------------------------------------------------------------------------"); 
tw.WriteLine("// My comments"); 
tw.WriteLine("// Are go here"); 

provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions()); 
1

Poiché non è possibile farlo tramite le API fornite in CodeDom, ecco qualche codice che ho appena scritto per risolvere il problema per me. Non perfetto, ma fa il trucco.

var marker = "//------------------------------------------------------------------------------"; 
var allTheCode = sw.ToString(); 
var justTheRealCode = allTheCode.Substring(allTheCode.IndexOf(marker) + marker.Length, allTheCode.LastIndexOf(marker) + marker.Length); 
justTheRealCode = allTheCode.Substring(justTheRealCode.Length); 
1

Abbastanza kludgy, ma quando ho avuto bisogno di fare questo, ho creato una classe che avvolge il flusso di output e braciole fuori le prime dieci righe:

/// <summary> 
    /// Removes the first 10 lines from the output. This removes the junk from the .NET Code Generator. 
    /// </summary> 
    internal class CodeOutputHelper : TextWriter 
    { 
     private readonly TextWriter _Inner; 
     private int _CountDown = 10; 

     public CodeOutputHelper(TextWriter inner) 
     { 
      _Inner = inner; 
     } 

     public override void WriteLine(string s) 
     { 
      if(_CountDown-- <= 0) 
      { 
       _Inner.WriteLine(s); 
      } 
     } 

     public override void Write(string value) 
     { 
      if (_CountDown<=0) 
      _Inner.Write(value); 
     } 

     public override void Write(char value) 
     { 
      _Inner.Write(value); 
     } 

     public override Encoding Encoding 
     { 
      get 
      { 
       return _Inner.Encoding; 
      } 
     } 
    } 
} 
Problemi correlati