3

I want to compile different thing based on the visual studio version i'm using, something like

#if VISUAL_STUDIO_VERSION > 2015
eventH?.Invoke(this, EventArgs.Empty);
#else
if(eventH != null)
  eventH(this, EventArgs.Empty);
#endif

How can I do this?

I tried something like:

#if (_MSC_VER==1700)

based on this with no luck.

EDIT

I need it in a WPF application (obviously).

based on MSDN #if in c# are only boolean. So, is there a way to define a symbol in project properties based on VS version or something similar?

Community
  • 1
  • 1
rmbq
  • 427
  • 4
  • 20

2 Answers2

1

What you have mentioned is called a #define, a compile time constant. These defines need to be.... defined (duh!!), if they are not defined then they are null therefore the expression fails. Your original linked example was for C++, so naturally it isn't defined for you in C#.

Under the hood Visual Studio uses MSBuild to do the actual compilation of your projects (MSBuild then invokes the compiler). The Visual Studio version you are using isn't available to MSBuild. Even trying to use a pre-build macro will not work because that is executed in the context of MSBuild/TFSBuild, at that point in time Visual Studio isn't within the context.

I should also point out that you have confused the Visual Studio version with the compiler version - and the compiler version also isn't natively available as a define.

Your best option is to create and use a custom MSBuild/TFSBuild build definition, you can then set these #defines as parameters to the build and you can also control which compiler is invoked (although that is too complex to show as an answer here). You would have one build definition for targeting the C# 6 compiler, and one for the default compiler.

slugster
  • 49,403
  • 14
  • 95
  • 145
-2

You should create a new configuration. You can give it a custom name. You just need to select that configuration while building or publishing your project. And you can use that in the same way as you use #if Debug.

ex. # if MyCustomConfiguration

Below links may help you

Custom build configurations on VisualStudio

How to: Create and Edit Configurations

Ankush Jain
  • 5,654
  • 4
  • 32
  • 57