Detecting the use Mono CLR dynamically

While developing something that could be used on Mono on Windows, Mono on Unix and on Windows with Microsoft’s CLR, I needed to be sensitive to the environment but didn’t want to conditionally compile my code different. So I put together a quick class to help.. Below is the C# code with pics of it running on Windows/Mac…


using System;
using System.Reflection;

namespace Gennard.Net
{
public class CLRUtils
{
private static readonly bool isMono= Type.GetType("Mono.Runtime") == null ? false : true;

private static readonly int eOSp = (int)Environment.OSVersion.Platform;
private static readonly bool isUnix = (eOSp == 4) || (eOSp == 128);

/* Class Properties */
public static bool IsMono { get { return isMono; } }
public static bool IsUnix { get { return isUnix; } }

public static void Main()
{
Console.WriteLine("Are we using Mono? : "+IsMono);
Console.WriteLine("Are we using Unix? : "+IsUnix);
}
}
}

The taste of the pudding mix.. is in the eating.. so lets see it working…

On Windows....

On the Mac, we get....

This entry was posted in CLR, CSharp, Mono, Tips and tagged , , . Bookmark the permalink.