Skip to content

Instantly share code, notes, and snippets.

@kangaroo
Created April 13, 2010 15:08
Show Gist options
  • Save kangaroo/364712 to your computer and use it in GitHub Desktop.
Save kangaroo/364712 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;
namespace BlockTestiPad
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args);
}
}
// The name AppDelegate is referenced in the MainWindow.xib file.
public unsafe partial class AppDelegate : UIApplicationDelegate
{
delegate void MyDelegate (IntPtr thiz, IntPtr obj, ref bool stop);
delegate void copy_helper (IntPtr dst, IntPtr *src);
delegate void dispose_helper (IntPtr src);
[StructLayout (LayoutKind.Sequential)]
struct BlockDescriptor {
int reserved;
public int size;
IntPtr copy_helper;
IntPtr dispose;
}
[StructLayout (LayoutKind.Sequential)]
unsafe struct BlockLiteral {
public IntPtr isa;
public int flags;
public int reserved;
public IntPtr invoke;
public IntPtr block_descriptor;
}
unsafe static IntPtr MakeInt (MyDelegate del)
{
var descriptor = (BlockDescriptor *) Marshal.AllocHGlobal (sizeof (BlockDescriptor));
descriptor->size = sizeof (IntPtr) * 3 + sizeof (int) * 2;
var block = (BlockLiteral *) Marshal.AllocHGlobal (sizeof (BlockLiteral));
block->isa = Class.GetHandle ("__NSConcreteGlobalBlock");
block->invoke = Marshal.GetFunctionPointerForDelegate (del);
block->flags = 1 << 28;
GCHandle.Alloc (del);
return (IntPtr) block;
}
// This method is invoked when the application has loaded its UI and its ready to run
public override void FinishedLaunching (UIApplication application)
{
try {
Console.WriteLine ("fl");
var s = new NSMutableSet ();
s.Add (new NSString ("foo"));
s.Enumerate (MakeInt (delegate (IntPtr thiz, IntPtr obj, ref bool stop) {
Console.WriteLine ("Got {0}", Runtime.GetNSObject (obj));
}));
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
} catch (Exception ex) {
Console.WriteLine (ex);
}
window.MakeKeyAndVisible ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment