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.
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.
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.
NET Core is installed on Windows is: Press Windows + R. Type cmd. On the command prompt, type dotnet --version.
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.
NETFRAMEWORK, NET48, NET472, NET471, NET47, NET462, NET461, NET46, NET452, NET451, NET45, NET40, NET35, NET20
NETSTANDARD, NETSTANDARD2_1, NETSTANDARD2_0, NETSTANDARD1_6, NETSTANDARD1_5, NETSTANDARD1_4, NETSTANDARD1_3, NETSTANDARD1_2, NETSTANDARD1_1, NETSTANDARD1_0
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
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.
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
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