What is the recommended way to determine the Mono runtime version programmatically?
We have encountered various problems when our .Net application is used with older versions of Mono. Sometimes we can work around these problems, if we know which version we are dealing with, but sometimes we can't.
The solution for us would be to detect the Mono version programmatically, and then we can transparently apply the workarounds. If the Mono version is too old, then we would prompt the user to upgrade.
We can discover that Mono is our runtime with something like this:
bool isMonoRuntime = Type.GetType("Mono.Runtime") != null;
How can we also determine reliably the mono version without inferring it indirectly? To be clear, we need the Mono release number, not the .Net CLR version number.
You can check the mono runtime version as:
Type type = Type.GetType("Mono.Runtime");
if (type != null)
{
MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
if (displayName != null)
Console.WriteLine(displayName.Invoke(null, null));
}
Check this How to determine the revision from which current Mono runtime was built and installed? for more details.
Here is another solution (without using reflection)
[DllImport("__Internal", EntryPoint="mono_get_runtime_build_info")]
public extern static string GetMonoVersion();
Console.WriteLine("Mono version: {0}",GetMonoVersion());
Mono version: 3.12.0 (tarball Sat Feb 7 19:13:43 UTC 2015)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With