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