[Mono-osx] How to handle returned List<>

Duane Wandless duane at wandless.net
Tue Apr 10 11:35:39 UTC 2012


This is a post on how I embedded a Mono C# library in a Cocoa application.
http://ramblingsofcode.blogspot.com/2011/07/embedding-mono-runtime-in-cocoa-app.html

Then you properly export/register your C# classes and methods.  Then you
can call:

MonoCSharpClass* csharpObject = [CSharpStaticClass getAnObject];
NSArray* passinArray =
[csharpObject runAMethod:passinArray];
NSArray* getArray = [csharpObject returnNSArray];

In the C# side you'd have:
[Register("CSharpStaticClass"]
public static class CSharpStaticClass : NSObject
{
   [Export("getAnObject")]
   static public MonoCSharpClass GetAnObject() { .. }
}
[Register("MonoCSharpClass")]
public class MonoCSharpClass : NSObject
{
   [Export("runAMethod:")]
   public void RunAMethod(NSArray inArray) { .. }
   [Export("returnNSArray")]
   public NSArray ReturnNSArray() { .. }
}

You have to convert your List<int> into an NSArray.  I have it setup so I
generate .h files and .m/.h files for the static classes.  To the obj-c
programmer using the C# classes is exactly the same as using an objc-c
library.

Maybe that will help you.

Duane

On Sat, Apr 7, 2012 at 12:25 PM, liljo <lantoine.jo at gmail.com> wrote:

> I'm embedding Mono in an MacOSX app written in Objective-c.
>
> I'm accessing a C# lib (DDL), which only contains a bunch of static methods
> returning different types. So far I can successfully get a returned int,
> double and string, but I'm having trouble retrieving a returned array...
>
> For exemple, here's how I retrieve an int:
>
> MonoDomain *domain = mono_jit_init("TestDomain");
>
> NSBundle* mainBundle = [NSBundle mainBundle];
> NSString* dll = [mainBundle pathForResource:@"TestLib86" ofType:@"dll"];
>
> MonoAssembly* assembly = mono_domain_assembly_open(domain, [dll
> UTF8String]);
>
> MonoImage* image = mono_assembly_get_image(assembly);
>
> // Get INTEGER
>
> // get a method handle to whatever you like
> const char* descAsString = "MiniLib86.Show:GetInt()";
> MonoMethodDesc* description = mono_method_desc_new(descAsString,TRUE);
> MonoMethod* method = mono_method_desc_search_in_image(description, image);
>
> // call it
> void* args[0];
> MonoObject *result = mono_runtime_invoke(method, NULL, args, NULL);
> int int_result = *(int*)mono_object_unbox (result);
>
> // See the result in log
> NSLog(@"int result %i", int_result);
>
>
> The method in C# that returns an List looks like this:
>
> public static List<int> GetListInt()
> {
>    return new System.Collections.Generic.List<int>{1,2,3,4,5};
> }
>
> Any help would be really appreciated !
>
> --
> View this message in context:
> http://mono.1490590.n4.nabble.com/How-to-handle-returned-List-tp4539658p4539658.html
> Sent from the Mono - OSX mailing list archive at Nabble.com.
> _______________________________________________
> Mono-osx mailing list
> Mono-osx at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-osx
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-osx/attachments/20120410/cbe25761/attachment-0001.html>


More information about the Mono-osx mailing list