【问题标题】:How do I customize the auto-generated comment when using .NET CodeDom Code Generation?使用 .NET CodeDom 代码生成时如何自定义自动生成的注释?
【发布时间】:2011-01-18 09:27:22
【问题描述】:

我正在使用CodeCompileUnitCSharpCodeProvider 生成一些源代码。它将下面的标题添加到所有生成的代码中。有没有办法自定义评论,让它说别的东西?

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

【问题讨论】:

    标签: c# .net code-generation


    【解决方案1】:

    你不能。我建议在此之后立即添加您自己的评论。这是一个如何做到这一点的示例:http://www.codeproject.com/KB/dotnet/ResourceClassGenerator.aspx

    【讨论】:

      【解决方案2】:

      您可以简单地将您的 cmets 添加到文件的开头,如下所示:

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

      就在将 CompileUnit 生成到 TextWriter 之前:

      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());
      

      【讨论】:

        【解决方案3】:

        相当笨拙,但是当我需要这样做时,我创建了一个包装输出流并切断前十行的类:

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

        【讨论】:

          【解决方案4】:

          由于您无法通过 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);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-18
            • 2021-01-21
            • 2011-11-24
            • 2016-05-05
            • 2011-01-08
            • 2013-01-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多