Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I customize the auto-generated comment when using .NET CodeDom Code Generation?

I'm using CodeCompileUnit and CSharpCodeProvider to generate some source code. It adds the header below to all generated code. Is there a way to customize the comment so it says something else?

// <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>
like image 849
NotDan Avatar asked Feb 18 '10 15:02

NotDan


3 Answers

You can't. I recommend adding your own comment immediately after this one. Here's an example of how to do that: http://www.codeproject.com/KB/dotnet/ResourceClassGenerator.aspx

like image 191
Joel Rondeau Avatar answered Nov 03 '22 15:11

Joel Rondeau


You can simply add your comments at the beginning of the file to look like this:

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

Just before generating the CompileUnit to a TextWriter do:

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());
like image 3
Ruth M. Avatar answered Nov 03 '22 16:11

Ruth M.


Pretty kludgy, but when I needed to do this, I created a class that wraps the output stream and chops off the first ten lines:

    /// <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;
            }
        }
    }
}
like image 3
Phil S Avatar answered Nov 03 '22 17:11

Phil S