How can I conditionally compile my C# for Mono vs. Microsoft .NET?

后端 未结 2 1426
借酒劲吻你
借酒劲吻你 2020-11-27 04:09

I need a conditional compilation switch that knows if I am compiling for the mono or MS .NET runtime. How can I do this?

相关标签:
2条回答
  • 2020-11-27 04:22

    The Mono compiler defines __MonoCS__

    BUT, BUT, BUT, the whole point of Mono is that you can take an assembly that you built with VS and run it on Mono, or vice versa.

    It seems to me that if you need to have Mono vs MS.NET differences, then you need to be making those decisions at run-time.

    The standard way to detect Mono at Runtime is:

    bool runningOnMono = Type.GetType ("Mono.Runtime") != null;
    
    0 讨论(0)
  • 2020-11-27 04:42

    It is clear that there are times when code on one platform will differ to code on another and you may wish to do this at run-time or maybe compile time. Some times it can't be done at run-time.

    For example:

    Mono is used as the basis of Xamarin's Ios, Android, and Mac tool kits. While these have a lot of common code they also have classes which only appear on a single platform. When developing with these tool kits you develop native packages which are not exchangeable between platforms.

    A simple case is file names and paths. These differ on each platform. I just might to have a little condition to load the strings differently on each platform. Some platforms have case specific file names some don't.

    It would be nice if there was a little bit of code which returned the current platform - be it UNIX, iOS, Mac, X86, X64, XBox etc....

    0 讨论(0)
提交回复
热议问题