Reputation: 344
I have a solution which multi-targets net framework 4.8.1 and Net 6. The code needs to run on both Linux and Windows, so I need to bifurcate code with appropriate #if NET481 statements and this generates warnings in the net 6.0 target but not the framework target.
I only want to disable warnings within the file they occur in. I've tried doing something like this at the top of the files:
#if NET6_0
#pragma warning disable CSxxxx
#endif
But it doesn't seem to work- I still get the same warnings. If I disable warnings outside of an #if block, the warnings go away, but I don't want to disable the warnings Given the nature of the warnings (almost all Never Used or Never Assigned To) there is an onerous workaround (namely, to remove these items selectively using more #if statements), but I'd rather avoid that if possible, since it seems like this should work. Strangely, using the #warning statement works exactly as expected. So I suppose the question is, why aren't CS0649 and CS0169 behaving the same?
#if NET481
#pragma warning disable CS1030
//1030 is the #warning number
#endif
#warning Just Net6
#pragma warning restore CS1030
#if NET6_0
#pragma warning disable CS1030
#endif
#warning Just Net481
#pragma warning restore CS1030
I'd rather not just disable the warnings for both codebases, either- I do want to know when one of these warnings crops up in both, because then it requires investigation. I also would rather not add further bifurcation to the code- sectioning off the variables in question, and then where they're referenced and so on.
Upvotes: 0
Views: 269
Reputation: 344
After writing this question, I noticed that the behavior goes away upon a restart of Visual Studio 2022. I decided it was worthwhile to leave this question up in case anyone experiences similar behavior, as the restart, for me, seems to be required every time you try to disable a new type of warning or the same type in a new file.
Upvotes: 1