> On 17 Jul 2015, at 09:08, Uli Kusterer <[email protected]> wrote:
> 
> On 14 Jul 2015, at 19:48, Dave <[email protected]> wrote:
>> Does anyone know of a tool/framework that allows C# code to be compiled and 
>> called from Cocoa?
> 
> Dave,
> 
> are you aware of Elements (http://www.elementscompiler.com/elements/) ? It's 
> not quite your solution, but it allows creating a C# app that calls ObjC 
> methods and instantiates ObjC classes. So you could write your Cocoa code in 
> C# as well,

I think that the easiest way to write a C# mac app is with Xamarin.Mac.
http://developer.xamarin.com/guides/mac/getting_started/hello,_mac/#Main.cs
This provides bindings to AppKit and native support for C# assemblies.
From that point of view it is a great option if you are starting from scratch.

However, it does remove you somewhat from the Mac ecosystem and tool chain.
All of your UI code has to go through the Xamarin bindings (unless you want to 
make use of their Obj-C -> C# code gen tools at some point).

Professionally I want to work with the best native tools for the platform in 
question, especially on large projects that have a long projected lifespan.
For that and a few other reasons I decided to use a bridge.

> saving you a lot of work manually translating between languages at the 
> interface points.

The proposed Dubrovnik solution uses a T4 powered code generator  
- 
https://github.com/ThesaurusSoftware/Dubrovnik/blob/master/dotNET/Dubrovnik.Tools/Dubrovnik.Tools/Net2ObjC.tt
 - 
that loads up target C# assembles and spits out Obj-C code.
The above can be automated in a VS build phase.
This takes all the donkey work out of generating Obj-C -> C# bindings.

A sample output from the code gen:
https://github.com/ThesaurusSoftware/Dubrovnik/blob/master/dotNET/UnitTests/GeneratedObjC/Dubrovnik_UnitTests_ReferenceObject.m

Having said that C# is syntactically much more complex than Obj-C (Swift is 
very much more similar).
Managed generic objects and methods can be challenging to bridge to another 
language that has little or no equivalent feature ().
The unit test code more or less demonstrates the features supported by the 
bridge (I know that Obj-C recently got a generic collection bump).

https://github.com/ThesaurusSoftware/Dubrovnik/blob/master/Framework/XCode/Dubrovnik%20Unit%20Tests/Dubrovnik_Unit_Tests.m

Even you use Xamarin.mac to code in C# you are still using and interacting with 
App kit.
Objects being passed into Cocoa have to be rendered as Obj-C objects even 
though they started life as C# objects.

One point may illustrate the sort of subtleties that can arise.
A C# dictionary will maintain an integer 1 and a double 1.0 as separate keys in 
a managed dictionary.
An Objective-C NSDictionary using NSNumber keys won’t.

Dubrovnik uses a custom NSNumber subclass to provide compatible key behaviour 
when passing objects over the bridge.

Jonathan












_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to