Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

mono

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

like image 678
Frep D-Oronge Avatar asked Nov 30 '08 16:11

Frep D-Oronge


People also ask

What is conditional compiler in C?

Conditional compilation is the process of selecting which code to compile and which code to not compile similar to the #if / #else / #endif in C and C++. Any statement that is not compiled in still must be syntactically correct. Conditional compilation involves condition checks that are evaluable at compile time.

Why is conditional compilation used in C?

Conditional compilation provides a way of including or omitting selected lines of source code depending on the values of literals specified by the DEFINE directive. In this way, you can create multiple variants of the same program without the need to maintain separate source streams.

How #define works in C?

The #define creates a macro, which is the association of an identifier or parameterized identifier with a token string. After the macro is defined, the compiler can substitute the token string for each occurrence of the identifier in the source file.


1 Answers

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; 
like image 196
Will Dean Avatar answered Sep 21 '22 22:09

Will Dean