开发者

Isolate Mono-specific code

开发者 https://www.devze.com 2023-02-14 00:40 出处:网络
I\'m playing with adding Gtk# GUI to a Windows.Forms application. I need a way to isolate Mono-specific code in Program.cs since I\'d like to avoid creation of a separate .sln/.csproj. In C/C++/Object

I'm playing with adding Gtk# GUI to a Windows.Forms application. I need a way to isolate Mono-specific code in Program.cs since I'd like to avoid creation of a separate .sln/.csproj. In C/C++/Objective-C projects, I'd do something similar to #ifdef __APPLE__ or #ifdef _WIN32.

C# appears to have the #if command.

What is the typi开发者_运维知识库cal way to isolate Mono-specific code, or Visual Studio-specific code?


You can define a symbol using #define and check against it, using #if and #else.

You can also pass the symbol to the compiler using the /define compiler option.

See the complete list of C# Preprocessor directives here.

#define MONO // Or pass in "/define MONO" to csc 

#if MONO
 //mono specific code
#else 
 //other code
#endif

According to this SO answer, the mono compiler defines a __MonoCS__ symbol, so the following would work:

#if __MonoCS__
 //mono specific code
#else 
 //other code
#endif

The recommended method that the Mono "Porting to Windows" guide, as detailed in this answer by @Mystic, is:

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

This, of course, is a runtime check, versus the compile time checks above so may not work for your specific case.

0

精彩评论

暂无评论...
验证码 换一张
取 消