2010-02-18 20 views
11

Tôi đang sử dụng CodeCompileUnitCSharpCodeProvider để tạo một số mã nguồn. Nó thêm tiêu đề bên dưới vào tất cả mã được tạo. Có cách nào để tùy chỉnh nhận xét để nó nói điều gì khác không?Làm cách nào để tùy chỉnh nhận xét được tạo tự động khi sử dụng .NET CodeDom Code Generation?

// <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> 

Trả lời

0

Mặc dù điều này dường như không được CodeDOM hỗ trợ trực tiếp, bạn có thể sử dụng thực tế nhận xét này được phân cách rõ ràng bằng các thẻ <auto-generated></auto-generated>. Vì vậy, bạn có thể thay đổi nhận xét này chỉ đơn giản bằng cách thực hiện các hoạt động chuỗi trên đầu ra CodeDOM của:

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

Bạn chỉ có thể thêm bình luận của bạn ở phần đầu của tập tin trông như thế này:

//---------------------------------------------------------------------------- 
// 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> 
//---------------------------------------------------------------------------- 

Ngay trước khi tạo ra Biên dịchUnit thành một TextWriter làm:

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

Vì bạn không thể làm điều đó thông qua các API được cung cấp trong CodeDom, đây là một số mã tôi vừa viết để giải quyết vấn đề cho bản thân mình. Không hoàn hảo, nhưng không phải là lừa.

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

Khá kludgy, nhưng khi tôi cần phải làm điều này, tôi tạo ra một lớp học mà kết thúc tốt đẹp output stream và sườn ra khỏi mười dòng đầu tiên:

/// <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; 
      } 
     } 
    } 
} 
Các vấn đề liên quan