C# 使用.NET CodeDom代码生成时,如何自定义自动生成的注释?
我正在使用C# 使用.NET CodeDom代码生成时,如何自定义自动生成的注释?,c#,.net,code-generation,C#,.net,Code Generation,我正在使用CodeCompileUnit和CSharpCodeProvider生成一些源代码。它将下面的标题添加到所有生成的代码中。有没有一种方法可以自定义注释,使其显示其他内容 // <auto-generated> // This code was generated by a tool. // Runtime Version:2.0.50727.3053 // // Changes to this file may cause incorrect beh
CodeCompileUnit
和CSharpCodeProvider
生成一些源代码。它将下面的标题添加到所有生成的代码中。有没有一种方法可以自定义注释,使其显示其他内容
// <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>
//
//这段代码是由一个工具生成的。
//运行时版本:2.0.50727.3053
//
//对此文件的更改可能会导致不正确的行为,如果
//重新生成代码。
//
你不能。我建议在这一条之后立即添加您自己的评论。下面是一个如何执行此操作的示例:您只需在文件开头添加注释,如下所示:
//----------------------------------------------------------------------------
// 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>
//----------------------------------------------------------------------------
由于您无法通过CodeDom中提供的API来实现这一点,下面是我为自己编写的一些代码。虽然不完美,但却能达到目的
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);
非常麻烦,但当我需要这样做时,我创建了一个类来包装输出流,并删掉前十行:
/// <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;
}
}
}
}
//
///从输出中删除前10行。这将从.NET代码生成器中删除垃圾邮件。
///
内部类CodeOutputHelper:TextWriter
{
私有只读文本编写器(内部);
私人int_倒计时=10;
公共代码输出帮助器(TextWriter内部)
{
_内部=内部;
}
公共重写无效写线(字符串s)
{
如果(_)倒计时--
/// <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;
}
}
}
}