Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

suppress warning for generated c# code

Tags:

I have turned on "Treat warnings as errors" for my VS project which mean that I get errors for missing documentation (nice reminder for this particular project).

However, part of the code is generated by a custom tool which does not insert xml documentation so I'm looking for away to ignore the missing xml documentation for the generated code only, not for the entire project. I have no influence on the actual file generated and cannot really insert anything in the file (as it is regenerated frequently by the tool) so I looking for something existing outside the generated file (the classes that are generated are partial, if that helps)

like image 635
soren.enemaerke Avatar asked Apr 23 '10 20:04

soren.enemaerke


People also ask

How do you ignore a compiler warning?

In Solution Explorer, select the NuGet package you want to suppress compiler warnings for. From the right-click or context menu, select Properties. In the Suppress warnings box of the package's properties, enter the warning number you want to suppress for this package.

How do you supress a warning?

Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code. 1. @SuppressWarnings("unchecked") public class Calculator { } - Here, it will ignore all unchecked warnings coming from that class.

How do I stop a GCC warning?

To suppress this warning use the unused attribute (see Variable Attributes). This warning is also enabled by -Wunused , which is enabled by -Wall . Warn whenever a static function is declared but not defined or a non-inline static function is unused.

Which number is used to suppress the warning in a code?

Use a #pragma warning (C#) or Disable (Visual Basic) directive to suppress the warning for only a specific line of code.


2 Answers

The best way to avoid having warnings or Code Analysis error on generated code is to decorate your generated classes with the GeneratedCodeAttribute and make the code file ends with the *.generated.cs pattern.

If your code files also have a file header, you should had these tags:

//---------------------- // <auto-generated> //     Tool description // </auto-generated> //---------------------- 

This is not mandatory, but if you have code file header it is a good practice.

This way, FxCop and other tools like StyleCop will not analyse your code anymore.

What is abnormal is that your code generation tool is not decortating your code elements with the attribute mentioned above. Try to look if there is an option to enable in your tool settings or contact the developing team.


EDIT: Does the generated classes are partial classes and do the actual classes name and number changes often? Because if the generated code content is not moving a lot, what you can do is simply create another code file and just declare the generated partial class to decorate them with the GeneratedCodeAttribute. One time it saved my life (and my time!).

like image 163
Ucodia Avatar answered Sep 23 '22 02:09

Ucodia


EDIT: See comments indicating that this doesn't work with C# 4. I'm not clear whether it worked in earlier versions of the compiler. The C# 4 spec makes this pretty clear though. Section 2.5.8.1 states:

A #pragma warning restore directive restores all or the given set of warnings to the state that was in effect at the beginning of the compilation unit. Note that if a particular warning was disabled externally, a #pragma warning restore (whether for all or the specified warning) will not re-enable that warning.

Jeff has a workaround in a blog post - basically to reprocess autogenerated code as part of the build.


As Tom says, you can add an "ignore" to the whole project (Build / Suppress Warnings - enter 1591 as the warning number) - but then you can restore the warning yourself at the top of each of your non-generated files:

#pragma warning restore 1591 

It's pretty ugly, but it works (I've just tested it).

like image 32
Jon Skeet Avatar answered Sep 22 '22 02:09

Jon Skeet