Reputation: 37222
I am using T4 to generate some code. The code will be in a class called "MyClass.generated.cs" and typical output will look like this.
//<autogenerated/>
namespace MyNamespace
{
using System;
using System.CodeDom.Compiler;
[GeneratedCode("T4", "1.0.0.0")]
public partial class MyClass: SomeBaseClass
{
private SomeBaseClass myBackingField;
}
}
However, even though the class is decorated with the GeneratedCodeAttribute, I still get a Code Analysis warning as follows:
Field 'MyNamespace.MyClass.myBackingField' is never assigned to, and will always have its default value null
I have ensure that the Project Properties → Code Analysis → "Suppress results from generated code (managed only)" checkbox is checked.
Please note that I understand the meaning of the warning - I just want to know how to suppress it :)
Possible solutions I could modify my generator to use Suppressions to suppress specific warnings, but this is extra work that I shouldn't have to do (as generated code should be ignored by Code Analysis).
Related Questions
EDIT with background context
The actual generated code is essentially a wrapper around SomeBaseClass
. There are 100+ types in a namespace, and I want to change the behaviour of a subset of those. There are other warnings being generated as well - I just used this one as an example. Consider for example, if there is a property SomeBaseClass.MyObsoleteProperty
, which is decorated with the ObsoleteAttribute
. My code generater would still create a MyClass.MyObsoleteProperty
which would raise a Code Analysis warning.
Another example would be where the SomeBaseClass
(which is from a 3rd-party) would itself raise Code Analysis warnings if they had bothered to check for them (maybe the class is not CLS-compliant, for example). My wrapper will recreate any errors they have (and that would actually be the desired behaviour).
Upvotes: 0
Views: 1378
Reputation: 1342
I think you mean
#pragma warning disable
// generated code
#pragma warning restore
the "warning-list" is a placeholder in MSDN documentation for something like "c0605,c0403,c3498" etc
Upvotes: 0
Reputation: 37222
I figured it out - this is not a Code Analysis warning - it's a compiler warning.
Therefore, the only way to disable it is to modify the generator to enclose the class in pragma directives to suppress compiler warnings, e.g
#pragma warning disable warning-list
// Now generate some code
#pragma warning restore warning-list
WARNING Note that this is a dangerous feature - compiler warnings are there for a reason! Try and limit your use of it to as small a section as possible.
More information can be found at Suppressing "is never used" and "is never assigned to" warnings in C#
List of compiler warnings and errors here.
Upvotes: 2