I should like to know if arbitrary C# code can be made conditional on the compiler version. In particular, I should like to suppress Code Analysis warnings in the MSVS 2010 C# compiler that I know are not given by the 2013 compiler; I want to do this case by case, thus presumably in the source code.
While interested in this problem, I really want a general answer
MSVS 2010 gives a CA2204 (“Correct the spelling…”) warning about a class name in a string literal, while 2013 lets me eliminate this with the <Compound> element in the Custom Dictionary. The 2010 documentation suggests it only supports the elements <Dictionary> — <Words> — <Recognized> — <Word> (but it seems to have no problem using a dictionary including <Compound>).
I programme and test partly in MSVS 2013 (where various checks are better) and partly in 2010 (which is on our best test server). I have project definitions for both compilers. Ideally, I want 2010 to detect problems promptly rather than waiting till I compile with 2013, so I would like to suppress as little as possible, rather than a blanket suppression of CA2204 in the project.
I therefore thought of including a [CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204…"…] attribute conditional on something like #if !MSVS2013, but it appears no such feature tests are feasible.
In decreasing order of satisfactoriness I have considered the following, and would be interested to hear what others think works best:
MSVS2013 in MSVS projects for any compiler of at least that level and suppress as above. A bit awkward, but workable.Microsoft.Build.Tasks.Csc.LangVersion as alluded to in this answer. Sounds powerful but tricky to set up, have not investigated further.class SuppressMSVS2010MessageAttribute : SuppressMessageAttribute in 2010 which is a dummy in 2013. [SuppressMessage] is sealed!Can C# code be conditional on the compiler (or language) version?
Sure. If you manually define a symbol according to the version of the compiler you're using. Otherwise, no.
From the documentation:
The C# compiler itself defines no symbols or macros that you can use in your source code; all symbol definitions must be user-defined.
I.e. "no symbols" includes there also being no pre-defined symbol that would allow you to determine the compiler version and thus conditionally control compilation.
Note: naturally, "manually define" does not preclude you authoring your own build process, in which you include a build step that inspects the csc.exe program to determine its version and then defines some symbol which you use to control compilation. This is still "manual" as far as the compiler itself is concerned, but you can certainly automate it yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With