Visual studio #if on visual studio version

rmbq

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?

slugster

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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to check version of Visual Studio

分類Dev

Update Visual Studio 2017 to Preview version

分類Dev

visual studio version for XP users support ssis

分類Dev

Switch DNX Version in Visual Studio 2015?

分類Dev

Visual Studio 2015 not finding Visual studio 2010

分類Dev

How do you change the version of R running in Visual Studio 2017?

分類Dev

How to remove the horizontal white lines in Visual Studio 2017 version 15.8?

分類Dev

Windows START command loading wrong version of Visual Studio

分類Dev

I cannot install any version of Visual Studio in Windows 8.1

分類Dev

Using Visual Studio 2012 Express Version to reference dll

分類Dev

How can CMake determine the version of Visual Studio 2010?

分類Dev

Visual Studio:ContextSwitchDeadlock

分類Dev

Visual Studio Windows Emulator

分類Dev

Visual Studio 2017 crash

分類Dev

How to rename Visual Studio

分類Dev

KeyCloak Visual Studio

分類Dev

Visual Studio Properties Panel

分類Dev

Visual Studio SDK

分類Dev

LOOPING GUI in Visual Studio

分類Dev

Ajax request in visual studio

分類Dev

Using gallio in visual studio

分類Dev

Visual Studio debugging WebApi

分類Dev

Visual Studio 2015

分類Dev

Visual Studio for C#

分類Dev

Trouble with Python on Visual Studio

分類Dev

Downgrade Visual Studio 2019 back to Visual Studio 2017

分類Dev

Open Visual Studio 2008 Solution in Visual Studio 2013 without Upgrading

分類Dev

##[warning]Visual Studio version '14.0' not found. Falling back to version '15.0'

分類Dev

Visual Studio build error "This version of NuGet.exe is not compatible with the version of Windows you're running."

Related 関連記事

ホットタグ

アーカイブ