Re: C Sharp?
Re: C Sharp?
- Subject: Re: C Sharp?
- From: Jonathan Mitchell <email@hidden>
- Date: Fri, 17 Jul 2015 10:14:09 +0100
> On 17 Jul 2015, at 09:08, Uli Kusterer <email@hidden> wrote:
>
> On 14 Jul 2015, at 19:48, Dave <email@hidden> 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 Unit Tests/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@hidden)
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:
This email sent to email@hidden