How can I conditionally compile my C# for Mono vs. Microsoft .NET?
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;
Compiler #if directive to split between Mono and .NET
Well you could certainly use
#if MONO
and then compile with
gmcs -define:MONO ...
(Or put it in your Mono build configuration, of course. It really depends on how you're building your library.)
... what are you looking for beyond that?
C# determining if compiled with mono
Determining the platform at runtime is preferred thing to do. It's not uncommon that program is compiled with Mono's C# compiler and run on .NET (it's even possible to run Mono's C# compiler straight on .NET runtime).
__MonoCS__
is intended for compiler workarounds only and should never be used to limit target platform.
Isolate Mono-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.
Targeting individual platforms with Visual Studio
#define/#if blocks are one method for doing this. There is also a conditional attribute. You might also want to look into MSBuild targets.
You might also want to think about your class design and whether you can share have shared interfaces that enable more of a provider or "plug-in" model to support different platforms. Microsoft developed a Portable Libraries project type that might help with this.
Targeting individual platforms with Visual Studio
#define/#if blocks are one method for doing this. There is also a conditional attribute. You might also want to look into MSBuild targets.
You might also want to think about your class design and whether you can share have shared interfaces that enable more of a provider or "plug-in" model to support different platforms. Microsoft developed a Portable Libraries project type that might help with this.
Does C# (Mono) have platform specific macros
We can use Environment.OSVersion instead of macros.
Conditional compilation symbol for a .NET Core class library
Conditional variables should be defined in your project.json file for RC2, and I have a sample project here,
Port #SNMP from .NET Core RC1 to RC2
But there are also predefined ones from this article,
Developing Libraries with Cross Platform Tools
Related Topics
Getting a System.Type from Type's Partial Name
Generating Xml File Using Xsd File
Differencebetween Casting and Conversion
Check If Property Has Attribute
C#:Is Variance (Covariance/Contravariance) Another Word for Polymorphism
How to Install a Certificate into the Local MAChine Store Programmatically Using C#
How to Catch SQLserver Timeout Exceptions
How to Set the Wix Installer Version to the Current Build Version
Write to Windows Application Event Log Without Event Source Registration
Converting File into Base64String and Back Again
How to Get the Directory from a File's Full Path
Passing an Enum Value as Command Parameter from Xaml
Weak Event Handler Model for Use with Lambdas
Datetime Format to SQL Format Using C#
How to Tell When Httpclient Has Timed Out