如何告诉我的代码在Mono上运行?

Sim*_*mon 14 .net mono

可能重复:
如何检测正在使用的.NET运行时(MS与Mono)?

在.net中我怎么知道我的代码是否在Mono上运行?

Ada*_*rth 18

从单声道常见问题解答:

http://www.mono-project.com/FAQ:_Technical

以下是该链接:

如何检测我是否在Mono中运行?

拥有依赖于底层运行时的代码被认为是错误的编码风格,但有时这些代码是解决运行时错误所必需的.检测Mono的支持方式是:

using System;

class Program {
    static void Main ()
    {
        Type t = Type.GetType ("Mono.Runtime");
        if (t != null)
             Console.WriteLine ("You are running with the Mono VM");
        else
             Console.WriteLine ("You are running something else");
    }
}
Run Code Online (Sandbox Code Playgroud)

任何其他hack,例如检查System.Int32或其他corlib类型的基础类型,将来都注定要失败.

它的长短不一样.


Mat*_*ten 11

从单声道移植指南:

public static bool IsRunningOnMono ()
{
  return Type.GetType ("Mono.Runtime") != null;
}
Run Code Online (Sandbox Code Playgroud)