Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
774 views
in Technique[技术] by (71.8m points)

.net - Are there any formalized target framework directives?

I've read Microsoft's article about how to detect target framework, for example :

netcoreapp2.2
net47
net58 

But there are situations where I don't care about the exact version, but the general framework target :

NETCORE
.Net Framework

But I didn't find such flags.

Question:

Are there any generalized flags for that ? or better, how can I distinguish between those two without specifying all options?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Besides the version-specific directives such as NETSTANDARD2_0, the Target frameworks documentation lists the following preprocessor symbols:

  • NETFRAMEWORK

  • NETSTANDARD

  • NETCOREAPP

...and NETMF for .NET Micro Framework, WP for Windows Phone, and UAP for Universal Windows Platform.


Here is an example .csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>   
    <TargetFrameworks>netstandard1.5;netstandard2.0;net48;netcoreapp3.0</TargetFrameworks>
  </PropertyGroup>
</Project>

and C# class:

namespace ClassLibrary1
{
    public class Class1
    {
        public void Test()
        {
#if NETFRAMEWORK
            System.Console.WriteLine(".NET Framework");
#elif NETCOREAPP
            System.Console.WriteLine(".NET Core");
#elif NETSTANDARD
            System.Console.WriteLine(".NET Standard");
#endif
        }
    }
}

Here's .NET Core 3.0 selected (see top left), with the NETCOREAPP region 'live' and the others greyed out:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...