6

I have a couple of classes that have fields that are only assigned or used through reflection. As a result, the fields lead to the following warnings:

CS0169: The field [...] is never used
CS0649: Field [...] is never assigned to, and will always have its default value 0

A solution would be, to use the SuppressMessageAttribute on all of these classes, but this seems unclean, as the relevant classes already have a custom attribute.

I'd prefer something like this:

[KeepMyFields]
class SomeClass
{
    int usedField;
}

class KeepMyFieldsAttribute : System.Diagnostics.CodeAnalysis.SuppressMessageAttribute
{
    // [...]
}

However, SuppressMessageAttribute is sealed. What other options do I have, other than adding a code snippet to each relevant class? Might a custom MSBuild task be suitable, or is there a simpler way?

Al Gebra
  • 173
  • 1
  • 7
  • 1
    See here: http://stackoverflow.com/a/3821035/5311735 – Evk Jun 04 '16 at 13:03
  • 1
    The solution proposed there is adding `#pragma` to each class. This does not seem like a clean way, neither does converting all fields to properties. – Al Gebra Jun 04 '16 at 16:33

1 Answers1

4

If you are looking for "clean" solution, you can suppress all warnings of this type in entire project, in project build options: enter image description here Of course, this will hide warnings for other classes as well (which you probably don't want), but you can move all problematic classes to one separate project and apply this setting to it.

wlodziu
  • 81
  • 5