Tuesday, May 5, 2009

System.CodeDom.GeneratedCodeAttribute for suppressing the generated code warnings

For one of the utilities i am writing i am using the resx file to store various blocks of code and generating a csharp class file based on a xml file. Because of dynamic generation of blocks i am not able to indent the code properly and hence when i compile this code i obviously get style cop warnings in my project.
When i investigated further i saw that style cop warnings are not displayed in the designer files generated by Visual Studio for ASP.NET and WinForms application. One of the differences i found in the files generated by Visual Studio and files generated by my utility was that visual studio puts a tag <auto-generated> in the file header while i put a tag called <autogenerated/> in mine.
Any ways changing this didn't affect it at all and the warnings were still generated in the compilation log.
So i put a System.CodeDom.GeneratedCode attribute on my class and bingo all the warnings were happily ignored by the compiler.

So if you are writing any tool to auto generate the code use this attribute to suppress all the warnings from the auto generated code. Of course you need to be sure that warnings are not harmful and are ignorable warnings (In my case they were all stylecop warnings and no fxcop warnings)

~Abhishek

No comments: