Reputation: 870
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.
Upvotes: 7
Views: 1726
Reputation: 57169
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).
Upvotes: 1
Reputation: 63
The .NET Framework 4 hosting API provides the CLRCreateInstance function, which can return the ICLRMetaHost interface. You can then call the GetRuntime method on this interface to get a specific ICLRRuntimeInfo interface, given a particular CLR version. This procedure supersedes the CorBindToRuntimeEx method that is used by the .NET Framework 2.0 hosting API.
The .NET Framework version 2.0 hosting API provides the CorBindToRuntimeEx function to initialize the runtime. You can choose which version of the runtime to load, but a process can host only one version. If version 2.0, 3.0, or 3.5 is loaded, the function returns the ICLRRuntimeHost interface, which is used to start the runtime and execute managed code.
Source: http://msdn.microsoft.com/en-us/library/dd380850.aspx
Upvotes: 0