如何告诉我的代码在Mono上运行? [重复]

时间:2022-07-30 07:20:59

Possible Duplicate:
How to detect which .NET runtime is being used (MS vs. Mono)?

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

In .net how dow I tell if my code is running on Mono?

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

2 个解决方案

#1


18  

From the Mono FAQ:

从单声道常见问题解答:

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

Below is directly from that link:

以下是该链接:

How can I detect if am running in Mono?

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

Having code that depends on the underlying runtime is considered to be bad coding style, but sometimes such code is necessary to work around runtime bugs. The supported way of detecting Mono is:

拥有依赖于底层运行时的代码被认为是错误的编码风格,但有时这些代码是解决运行时错误所必需的。检测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");
    }
}

Any other hack, such as checking the underlying type of System.Int32 or of other corlib types, is doomed to fail in the future.

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

Long and short of it, just don't.

它的长短不一样。

#2


11  

From mono porting guide:

从单声道移植指南:

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

#1


18  

From the Mono FAQ:

从单声道常见问题解答:

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

Below is directly from that link:

以下是该链接:

How can I detect if am running in Mono?

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

Having code that depends on the underlying runtime is considered to be bad coding style, but sometimes such code is necessary to work around runtime bugs. The supported way of detecting Mono is:

拥有依赖于底层运行时的代码被认为是错误的编码风格,但有时这些代码是解决运行时错误所必需的。检测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");
    }
}

Any other hack, such as checking the underlying type of System.Int32 or of other corlib types, is doomed to fail in the future.

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

Long and short of it, just don't.

它的长短不一样。

#2


11  

From mono porting guide:

从单声道移植指南:

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