Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know which Runtime Host is currently run my code?

As Microsoft Documentation declare Runtime Hosts that .NET have more than one Runtime Hosts to support and execute the code of our application, my question is How Can I know which Runtime Hosts of the Microsoft Runtime hosts, is hosting my code.

I am using C# language to develop dll class library which may be used and/or hosted by various Runtime hosts, so I need to know which Runtime host is now hosting my code to satisfy specify conditions.

like image 705
Niklaus Wirth Avatar asked Jun 11 '13 09:06

Niklaus Wirth


People also ask

What is runtime host?

The runtime host loads the runtime into a process, creates the application domains within the process, and loads user code into the application domains.


1 Answers

There's actually quite an easy way to determine the current runtime version of the CLR. As it happens, Environment.Version will return a different version if your code is currently run in a different CLR due to SxS (Side-by-Side) execution.

To see how that works in practice in an application that can have two runtimes at the same time, check out this article on Demonstrating Side by Side execution.

if(Environment.Version.StartsWith("2.0"))
    System.Console.WriteLine("Inside .NET CLR 2.0");
else if(Environment.Version.StartsWith("4.0"))
    System.Console.WriteLine("Inside .NET CLR 4.0");
else
    System.Console.WriteLine("Unknown .NET version");

Note that the .NET 2.0 loader will load the most recent CLR of .NET 2.0 available, which will be .NET 3.5 in most cases. It is not possible to run different versions of .NET 2.0 side by side in one process. Neither is it possible to run .NET 1.0 or .NET 1.1 side by side with .NET 2.0 in .NET 4.0 (it is possible though to run just 1.0 or 1.1 side by side in .NET 4.0).

like image 185
Abel Avatar answered Sep 19 '22 11:09

Abel