简体   繁体   English

如何有条件地为Mono与Microsoft .NET编译C#?

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

I need a conditional compilation switch that knows if I am compiling for the mono or MS .NET runtime. 我需要一个条件编译开关,该开关知道我要为单声道还是MS .NET运行时进行编译。 How can I do this? 我怎样才能做到这一点?

The Mono compiler defines __MonoCS__ Mono编译器定义__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. BUT,BUT,BUT ,Mono的全部意义在于,您可以使用用VS构建的程序集并在Mono上运行它,反之亦然。

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. 在我看来,如果您需要在Mono与MS.NET之间存在差异,那么您需要在运行时做出这些决定。

The standard way to detect Mono at Runtime is: 在运行时检测Mono的标准方法是:

bool runningOnMono = Type.GetType ("Mono.Runtime") != null;

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. Mono被用作Xamarin的Ios,Android和Mac工具包的基础。 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.... 如果有一些代码返回当前平台,那就好了-UNIX,iOS,Mac,X86,X64,XBox等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM