Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set .NET Core in #if statement for compilation

I created a multi-targeted framework project. I use something like this:

  #if NET40     Console.WriteLine("Hello from .NET Core 4");   #endif 

But I can't find a wildcard for .NET Core. I tried:

   #if NETCOREAPP1.0      Console.WriteLine("Hello from .NET Core");    #endif 

But it is not valid statement.

like image 210
Serhii Shemshur Avatar asked Jul 20 '16 09:07

Serhii Shemshur


People also ask

How do I get .NET Core in Visual Studio?

NET Core can be installed in two ways: By installing Visual Studio 2017/2019 or by installing . NET Core Runtime or SDK. . NET Core installer already contains ASP.NET Core libraries, so there is no separate installer for ASP.NET Core.

How do I open .NET Core app?

Open Visual Studio 2019 and click on Create a new project, as shown below. The "Create a new project" dialog box includes different . NET Core 3.0 application templates. Each will create predefined project files and folders depends on the application type.

How do I know if I have NET Core 3.1 installed?

NET Core is installed on Windows is: Press Windows + R. Type cmd. On the command prompt, type dotnet --version.


2 Answers

You need an underscore, _, instead of a point:

NETCOREAPP1_0 or the more recent NETCOREAPP1_1 and NETCOREAPP2_0

The documentation for Target frameworks in SDK-style projects includes a list for the different preprocessor symbols.

.NET Framework

NETFRAMEWORK, NET48, NET472, NET471, NET47, NET462, NET461, NET46, NET452, NET451, NET45, NET40, NET35, NET20

.NET Standard

NETSTANDARD, NETSTANDARD2_1, NETSTANDARD2_0, NETSTANDARD1_6, NETSTANDARD1_5, NETSTANDARD1_4, NETSTANDARD1_3, NETSTANDARD1_2, NETSTANDARD1_1, NETSTANDARD1_0

.NET 5+ and .NET Core

NET, NET6_0, NET6_0_ANDROID, NET6_0_IOS, NET6_0_MACOS, NET6_0_MACCATALYST, NET6_0_TVOS, NET6_0_WINDOWS, NET5_0, NETCOREAPP, NETCOREAPP3_1, NETCOREAPP3_0, NETCOREAPP2_2, NETCOREAPP2_1, NETCOREAPP2_0, NETCOREAPP1_1, NETCOREAPP1_0

Mono

For Mono you can usually use the NetFramework monikers known by your version of Mono. For instance, Mono 6.12 includes all NetFramework versions from 2.0 to 4.8. But if you must recognise Mono per se, then MONO and __MonoCS__ should both be defined.

like image 183
Ralf Bönning Avatar answered Sep 20 '22 19:09

Ralf Bönning


Extension of Devon's answer for Visual Studio 2017 .csproj files:

Looking at the table here, you can easily define constants by using regular expressions. So you don't need to think about updating the conditions if target frameworks are added/changed.

<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^net\d'))">   <DefineConstants>NETFRAMEWORK</DefineConstants> </PropertyGroup> <PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^netstandard\d'))">   <DefineConstants>NETSTANDARD</DefineConstants> </PropertyGroup> <PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^netcoreapp\d'))">   <DefineConstants>NETCORE</DefineConstants> </PropertyGroup> 

Usage:

#if NETFRAMEWORK     FrameworkSpecific(); #endif  #if NETSTANDARD     StandardSpecific(); #endif  #if NETCORE     CoreSpecific(); #endif 
like image 32
Chris Avatar answered Sep 22 '22 19:09

Chris