问题
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)
回答1:
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).
回答2:
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!).
回答3:
The best you will be able to do in this is suppress the particular warning in the project that contains the generated code. You can do this in the Project Properties on the Build tab. It's not ideal, but short of putting the pragmas in the generated code, it is the best you can do.
来源:https://stackoverflow.com/questions/2701706/suppress-warning-for-generated-c-sharp-code