Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 555
Doctor Jones Avatar asked Oct 19 '25 21:10

Doctor Jones


1 Answers

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.

like image 63
CodeCaster Avatar answered Oct 22 '25 11:10

CodeCaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!