简体   繁体   中英

Target Framework Symbol not defined

I'm attempting to use an if compiler directive to conditionally include some code that is required for earlier versions of the .Net Framework, but not later versions.

I've used the following references from MSDN, the #if compiler directive reference, and the Target Frameworks reference.

I'm authoring a .Net 4.5.1 application, but the NET451 symbol doesn't appear to be defined, and neither do any others.

I've adapted the example from the Target Frameworks MSDN article to include all framework symbols, so I can see which one is defined.

#if NET20
        Console.WriteLine("Target framework: NET20");
#elif NET35
        Console.WriteLine("Target framework: NET35");
#elif NET40
        Console.WriteLine("Target framework: NET40");
#elif NET45
        Console.WriteLine("Target framework: NET45");
#elif NET451
        Console.WriteLine("Target framework: NET451");
#elif NET452
        Console.WriteLine("Target framework: NET452");
#elif NET46
        Console.WriteLine("Target framework: NET46");
#elif NET461
        Console.WriteLine("Target framework: NET461");
#elif NET462
        Console.WriteLine("Target framework: NET462");
#elif NET47
        Console.WriteLine("Target framework: NET47");
#elif NET471
        Console.WriteLine("Target framework: NET471");
#elif NET472
        Console.WriteLine("Target framework: NET472");
#elif NETSTANDARD1_0
        Console.WriteLine("Target framework: NETSTANDARD1_0");
#elif NETSTANDARD1_1
        Console.WriteLine("Target framework: NETSTANDARD1_1");
#elif NETSTANDARD1_2
        Console.WriteLine("Target framework: NETSTANDARD1_2");
#elif NETSTANDARD1_3
        Console.WriteLine("Target framework: NETSTANDARD1_3");
#elif NETSTANDARD1_4
        Console.WriteLine("Target framework: NETSTANDARD1_4");
#elif NETSTANDARD1_5
        Console.WriteLine("Target framework: NETSTANDARD1_5");
#elif NETSTANDARD1_6
        Console.WriteLine("Target framework: NETSTANDARD1_6");
#elif NETSTANDARD2_0
        Console.WriteLine("Target framework: NETSTANDARD2_0");
#elif NETCOREAPP1_0
        Console.WriteLine("Target framework: NETCOREAPP1_0");
#elif NETCOREAPP1_1
        Console.WriteLine("Target framework: NETCOREAPP1_1");
#elif NETCOREAPP2_0
        Console.WriteLine("Target framework: NETCOREAPP2_0");
#elif NETCOREAPP2_1
        Console.WriteLine("Target framework: NETCOREAPP2_1");
#elif NETCOREAPP2_2
        Console.WriteLine("Target framework: NETCOREAPP2_2");
#else
        Console.WriteLine("Could not tell which framework we're using.");
#endif

The resulting output is "Could not tell which framework we're using".

Am I doing something wrong? Is the documentation incorrect? Or have I found a bug?

These directives are only implicitly generated by the build system for .NET Core. Keyword from the documentation :

The complete list of preprocessor symbols for .NET Core target frameworks is:

So when you create a new application or class library targeting .NET Framework, the directives will be missing. Instead create a Core or .NET Standard library or application.

If you're not ready to move to Core, see Detect target framework version at compile time for workarounds - you could define them yourself, for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM