I would like to use larger body of C# code as a library for Objective-C (Cocoa) application.
I discovered MonoMac project which wraps Cocoa code, but I would rather have standard Cocoa application written in Objective-C, which can call wrapped C# code (other way around).
On Windows I am used to make C++/CLI project which wraps .NET code and exports plain old C interface for C/C++ based apps.
Is there some simple way to achieve this?
There is, obviously, no such language as C++/CLI on Mac OS. On Windows, C++/CLI actually compiles as managed code ran by the CLR, that runs native code; since on Mac OS Mono isn’t integrated to the system, it’s rather the other way around. Your app is native, and it can host managed code.
Mono exposes functions to host a CLR virtual machine inside a process. Since CLR classes aren’t directly exposed to your C code, you’ll be able to call methods of objects through reflection-like calls.
There is documentation on how to embed Mono into an application on the official site. Since you’re not interested in running .NET programs directly, you should rather read the “Invoking Methods in the CIL Universe” section. On Mac OS, you’ll want to link against the Mono framework from your
/Library/Frameworksfolder, instead of usingpkg-config.This really shouldn’t replace an actual reading of the above document, but the following can be seen as a guide as to what to expect:
If you don’t find this very appealing, you may want to go the other way around, as you mentioned, and look into MonoMac, which provides .NET bindings to a large portion of the APIs you may want to use in a Mac application (Cocoa, CoreImage, CoreAnimation, etc) and means to create your own bindings.