Cómo detectar which.NET ¿se está utilizando el tiempo de ejecución (MS vs. Mono)?


Me gustaría saber durante la ejecución de un programa si se está ejecutando utilizando el tiempo de ejecución Mono o el tiempo de ejecución de Microsoft.

Actualmente estoy usando el siguiente código para determinar si estoy en un MS CLR:

static bool IsMicrosoftCLR()
{
    return RuntimeEnvironment.GetRuntimeDirectory().Contains("Microsoft");
}

Sin embargo, esto depende un poco de la carpeta de instalación del tiempo de ejecución y no estoy seguro de si funcionará en todas las instalaciones.

¿Hay una mejor manera de comprobar el tiempo de ejecución actual?

 43
Author: Dirk Vollmar, 2009-04-06

5 answers

De la Guía del Proyecto Mono para Portar Aplicaciones Winforms :

public static bool IsRunningOnMono ()
{
    return Type.GetType ("Mono.Runtime") != null;
}

Estoy seguro de que tendrá muchas más preguntas, así que vale la pena revisar esta guía y el mono-foros

 70
Author: Mystic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-04-06 12:24:37

Puede verificar el Tiempo de ejecución Mono de esta manera

bool IsRunningOnMono = (Type.GetType ("Mono.Runtime") != null);
 24
Author: Alex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-04-06 12:22:57

Simplemente ejecute el siguiente código..

static bool IsMicrosoftCLR()
{
    return (Type.GetType ("Mono.Runtime") == null)
}
 9
Author: Binoj Antony,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-04-06 12:21:50

Con el advenimiento de C# 6, esto ahora se puede convertir en una propiedad get-only, por lo que la comprobación real solo se realiza una vez.

internal static bool HasMono { get; } = Type.GetType("Mono.Runtime") != null;
 7
Author: RobinHood70,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-13 21:42:36

Aquí hay una versión con almacenamiento en caché que estoy usando en mi proyecto:

public static class PlatformHelper
{
    private static readonly Lazy<bool> IsRunningOnMonoValue = new Lazy<bool>(() =>
    {
        return Type.GetType("Mono.Runtime") != null;
    });

    public static bool IsRunningOnMono()
    {
        return IsRunningOnMonoValue.Value;
    }
}

Como @ahmet alp balkan mencionó, el almacenamiento en caché es útil aquí si está llamando a esto con frecuencia. Al envolverlo en un Lazy<bool>, la llamada de reflexión solo ocurre una vez.

 5
Author: Nate Barbettini,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-17 21:49:14