Take the 2-minute tour ×

We have started a new project but also have this problem for an existing project. The problem is that when we compile with a warning level of 4 we also want to switch on

'Treat all warnings as errors'

We are unable to do this at the moment because generated files (in particular reference.cs files) are missing things like XML comments and this generates a warning, we do not want to suppress the xml comment warnings totally out of all files just for specific types of files (namely generated code).

I have thought of a way this could be achieved but am not sure if these are the best way to do this or indeed where to start :) My thinking is that we need to do something with T4 templates for the code that is generated such that it does fill in XML documentation for generated code.

Does anyone have any ideas, currently I'm at well over 2k warnings (its a big project) :(

share|improve this question
You can set properties of individual source files, including compiler options for individual source files. Those will overrule properties inherited from the project level. –  Windows programmer Apr 30 '10 at 8:49
Really? How???? –  erikkallen Apr 30 '10 at 9:28
I did comment on the above but yes how is this achevied, could you give an example please. –  krystan honour Apr 30 '10 at 9:36
Sorry, I'm not sure if this can be done for C#. For C++, in the Solution Explorer window, you can right-click the filename of an individual file and set its properties. Sometimes I need to compile one source file with different options from others, and this works at least in C++. –  Windows programmer May 5 '10 at 23:40
yes that is not possible with c# i'm afraid. (not in an obvious right clicky way) –  krystan honour May 6 '10 at 15:47
add comment

5 Answers

up vote 2 down vote accepted

I've written a PowerShell script that calls svcutil and then wraps the auto-generated code with the #pragma directives to ignore the missing xml, but still allows me to regenerate as needed.

$outFile = 'generatedCode_fromSVCUTIL.cs'
svcutil '..\XML Schema\myXsd.xsd' /dataContractOnly /n:'*,MyNamespace.GeneratedCode' /language:C#  /importxmltypes /out:$outFile

# Exempt this file from XML documentation requirements

Write-Host 'Wrapping ', $outFile, ' in #pragma 1591 flags' 
$a = Get-Content $outFile 

# Set up pragma lines for enabling and disabling the XML doc warning
$disableWarning = '#pragma warning disable 1591'
$restoreWarning = '#pragma warning restore 1591'

# wrap the generated code in the pragma tags
Set-Content $outFile –value $disableWarning, $a, $restoreWarning 
Write-Host 'Done.' 
share|improve this answer
much better solution this. Changed this as the accepted answer as it does not involve changing visibility of objects. –  krystan honour Nov 20 '12 at 11:24
add comment

You can selectively disable warnings with a pragma:

// Disable warning messages 4507 and 4034.
#pragma warning( disable : 4507 34 )

If you can emit such warnings (or an #include) in the generated code files, you're done.

Alternatively, you can disable them globally on the command-line for the compiler:

/wd4326 disables compiler warning C4326.

Then re-enable them (via a header file) in the files you want them for:

// Report warning 4326 as an error.
#pragma warning( error : 326 )

Finally, you can set different compile options for each source file by altering the Properties in the project file. Personally I find that a maintenance nightmare, but there are times you have no choice.

Edit: I see that your source files are C#, not C++.

Using the C# command-line:

to suppress CS0028, you could specify /nowarn:28.

Unfortunately, /warnaserror makes all warnings errors.

share|improve this answer
Interesting, you say as does (Windows programmer) below that you can alter the properties on a per source file basis in the .csproj properties, I cannot find how this is acheived. is this done in the Suppress warnings: text box?, to be honest I pretty much want to ignore generated code. –  krystan honour Apr 30 '10 at 9:26
I'm afraid you'll have to use the C# command-line compiler (csc.exe). –  egrunin Apr 30 '10 at 16:54
The idea is that once you've added the #pragma warning disable, you're set. You just have to update the code generator once. Do you have to option of changing the code generator? –  Pieter van Ginkel Oct 18 '10 at 17:02
add comment

In VS 2010 you can right-click on the service reference, select 'Configure Service Reference...' and change the access modifier from Public to Internal.
This may of course not be appropriate for your particular solution but the warnings are not applicable to Internal methods and you can still re-generate the service reference.

share|improve this answer
add comment

For C# you can simply place a

#pragma warning disable 1591

at the beginning of the reference.cs file. Then the warning concerning missing XML documentation will not be issued.

But you have to do this every time, the file is regenerated (i.e. when your service definition changes). I'm not aware of any way to influence the code generation (I'm not sure if they use T4 templates or where these might be located ...)

share|improve this answer
yes thats rather the issue Its a maintainance nightmare having to do it every time, again it looks useful but I would want that to be put in every time if that were possible. –  krystan honour Apr 30 '10 at 9:28
add comment

A couple of thoughts.

1) Do you have the autogenerated tag in your file header (comments at the top of the file), like this:

// <auto-generated>

// This file is auto-generated...

// </auto-generated>

This tag is important (the contents are not), as some tools will skip such files (e.g. StyleCop can be configured to ignore these files).

2) If you are autogenerating code why not autogenerate at least some XML comments? I can understand that you don't want to spend a lot of time documenting code that probably won't ever be read, but when debugging code I often find myself dropping in to some autogenerated proxy and even a simple comment can be helpful, even if it just says "autogenerated code" :)


3) You can also suppress warnings by adding the pragmas to the build options (right click on the project, choose properties, choose the Build tab). This is more convenient than adding to code. Try adding 1591;1574;1587 to the Suppress Warnings box.

4) You could go in to the Code Analysis tab in the Project Properties and uncheck "Treat Warning as Error" for specific warnings that are causing you problems.

Obviously both these are global settings, they don't just pick on the autogenerated files.

share|improve this answer
I'm afraid this does not solve the issue, visual studio when generating the documentation appears to ignore the fact these headers exist. –  krystan honour Oct 18 '10 at 12:50
OK, added anither thought to my list:) –  Steve Haigh Oct 18 '10 at 13:45
I fail to see why this attracted down vote, could the downvoter at least have the courtesy to leave a comment saying what was wrong with this response? –  Steve Haigh Oct 27 '10 at 11:58
All of the comments here are usefull but unfortunately do no solve the issue at hand. The main issue here is that there seems to be no way to ignore warnings as errors on auto generated code easily, perhaps if there were an option in VS2010 to set this as custom code injection this would take care of the maintainance issue. Until such time that is possilble there will always be someone who regens the code and forgets the pragma which then breaks the build I was trying to automate that step. –  krystan honour Nov 2 '10 at 9:18
add comment

Your Answer


