问题
With the new MsBuild version it's really easy to target multiple frameworks and framework versions in .csproj project:
<TargetFrameworks>net40;net45;net461;netcoreapp1.1</TargetFrameworks>
Now, how do I get a constant defined for each of them so that I can have certain code only be part of e.g. netcoreapp1.1 pass and not full framework compilation?
I'd like to be able to add a preprocessor directive like this one:
#if NET_CORE
// some code
#endif
And the same for full .NET Framework and possibly .NET Standard.
The other questions I found don't take new <TargetFrameworks> way of defining the framework into accounts. Stuff like this doesn't work:
<DefineConstants Condition=" '$(TargetFramework)' == '.NETCoreApp' ">NET_CORE</DefineConstants>
回答1:
There are already various preprocessor symbols defined that you can use. From the docs:
.NET Framework 2.0 --> NET20
.NET Framework 3.5 --> NET35
.NET Framework 4.0 --> NET40
.NET Framework 4.5 --> NET45
.NET Framework 4.5.1 --> NET451
.NET Framework 4.5.2 --> NET452
.NET Framework 4.6 --> NET46
.NET Framework 4.6.1 --> NET461
.NET Framework 4.6.2 --> NET462
.NET Framework 4.7 --> NET47
.NET Framework 4.7.1 --> NET471
.NET Framework 4.7.2 --> NET472
.NET Standard 1.0 --> NETSTANDARD1_0
.NET Standard 1.1 --> NETSTANDARD1_1
.NET Standard 1.2 --> NETSTANDARD1_2
.NET Standard 1.3 --> NETSTANDARD1_3
.NET Standard 1.4 --> NETSTANDARD1_4
.NET Standard 1.5 --> NETSTANDARD1_5
.NET Standard 1.6 --> NETSTANDARD1_6
.NET Standard 2.0 --> NETSTANDARD2_0
.Net Core 1.0 --> NETCOREAPP1_0
.Net Core 1.1 --> NETCOREAPP1_1
.Net Core 2.0 --> NETCOREAPP2_0
.Net Core 2.1 --> NETCOREAPP2_1
For example:
#if NETSTANDARD1_6
Console.WriteLine("This is .Net Standard 1.6");
#endif
#if NETCOREAPP2_0
Console.WriteLine("This is .Net Core 2.0");
#endif
来源:https://stackoverflow.com/questions/42754123/conditional-per-targetframework-in-msbuild-15