1

We are attempting to do some multitargeting and having some bad things happening... I am no expert in this area so it's all greek to me.

In my experimenting, I'd like to find out from code, at runtime, which target framework was used to build the project.

Is there some way to do this?

Something like

if (TARGET_FRAMEWORK == "net462")
   Console.WriteLine("Hello World from .NET Framework version 4.6.2");
if (TARGET_FRAMEWORK == "net48")
   Console.WriteLine("Hello World from .NET Framework version 4.8");

Notes:

Environment.Version is the version of the CLR. A single CLR version typically supports multiple .NET Framework versions. CLR version greater than or equal to 4.0.30319.42000 supports .NET Framework versions starting with .NET Framework 4.6.

Running this code in a project set to 4.8 results in

namespace ConsoleApp48
{
    class Program
    {
        static void Main(string[] args)
        {
#if NET462
Console.WriteLine($"HelloWorld from NET462");
#elif NET48
Console.WriteLine($"HelloWorld from NET48");
#else
Console.WriteLine($"HelloWorld from UNKNOWN");
#endif

#if DEBUG
            Console.WriteLine("DEBUG");
#endif
            Console.ReadLine();
        }
    }
}

Results:

HelloWorld from UNKNOWN DEBUG

Rick the Scapegoat
  • 1,056
  • 9
  • 19
  • 1
    Do you need to know it at compile time or at runtime? – PMF May 12 '21 at 17:44
  • @PMF I am looking for runtime – Rick the Scapegoat May 12 '21 at 17:50
  • I think you can use [preprocessor directives](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives) – Guru Stron May 12 '21 at 17:54
  • That's quite tricky. Check other answers: https://stackoverflow.com/questions/8517159/how-do-i-detect-at-runtime-that-net-version-4-5-is-currently-running-your-code – PMF May 12 '21 at 17:54
  • @GuruStron: That's for compile time differences (i.e. if a class is not available in one but in another) – PMF May 12 '21 at 17:55
  • @PMF I know. But compile time is the time when "target framework was used to build the project" is selected. – Guru Stron May 12 '21 at 17:56
  • 1
    "The target framework that was used to build the project" is entirely different from "the version of the framework currently being used to run my code regardless of what it was targeting". Are you *sure* you're looking for the former and not the latter? And note that *neither* of these is "the version of the compiler that was used to build the project", which is only tangentially related to the framework version(s) but still relevant if (mis)compilation problems happen (which is admittedly much less common than JIT problems, but still). – Jeroen Mostert May 12 '21 at 18:00
  • @JeroenMostert I think you are right - I am more interested in the version used to compile the code. – Rick the Scapegoat May 12 '21 at 18:11
  • Only in necessary cases you need to know the compile time target frameworks (in SDK style projects, not legacy project format), https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/cross-platform-targeting#multi-targeting So whenever you want that, think twice to see if it is really necessary. – Lex Li May 12 '21 at 18:16
  • @GuruStron I thought this might be the answer, but it doesn't work. – Rick the Scapegoat May 12 '21 at 18:16
  • @RicktheScapegoat: Can you show what you've tried? Using the preprocessor directives is normally just working. – PMF May 12 '21 at 18:22
  • @PMF I updated the question with the preprocessor code I used. – Rick the Scapegoat May 12 '21 at 18:43
  • 1
    @OlivierRogier no it doesn't... I need the build version... not the CLR – Rick the Scapegoat May 12 '21 at 18:44
  • Does this answer your question? [How to find out Target framework name and version from exe?](https://stackoverflow.com/questions/62058953/how-to-find-out-target-framework-name-and-version-from-exe) and [Detect target framework version at compile time](https://stackoverflow.com/questions/3436526/detect-target-framework-version-at-compile-time) and [Retrieve Target Framework Version and Target Framework Profile from a .Net Assembly](https://stackoverflow.com/questions/6854664/retrieve-target-framework-version-and-target-framework-profile-from-a-net-assem) –  May 12 '21 at 18:53
  • @OlivierRogier Thanks for sticking with me... The requirements here are so fubar - but it's the cards I have been dealt. The "build version" is the .net framework used during compiling. Someone here, decided that 4.8 is the way to go for some projects we have, but not all. He wants to use multi-targeting so that during build the project that needs 4.8 references.. gets libraries compiled with 4.8 and the project that needs 4.6.2 gets the same shared libraries compiled with 4.6.2... – Rick the Scapegoat May 12 '21 at 18:57

0 Answers0