0

The result of calling method_getImplementation(method) is an IntPtr. How do I convert an IntPtr to a method?

[DllImport("/usr/lib/libobjc.dylib")]
extern static IntPtr method_getImplementation(IntPtr method);

public void SomeMethod
{
var method = class_getInstanceMethod(new UIViewController().ClassHandle, new Selector("viewWillAppear:").Handle);

IntPtr original_impl = method_getImplementation(method);
}

In obj-c:

IMP originalImp = method_setImplementation(method,swizzleImp);
originalImp(self,_cmd)
Derek Beattie
  • 9,429
  • 4
  • 30
  • 44
  • Gotta be honest, this looks like the sort of plumbing the interop layer should be handling, since in general you just flat-out can't call arbitrary memory locations from managed code. So maybe a bit of an XY problem? What are you trying to do that involves this? – Nathan Tuggy Feb 17 '15 at 06:31
  • This, http://stackoverflow.com/questions/14127453/how-to-port-method-getimplementation-and-method-setimplementation-to-monotou – Derek Beattie Feb 17 '15 at 07:12
  • Hmm, maybe this: http://stackoverflow.com/questions/12767903/create-delegate-at-runtime-from-native-method-on-ios – Derek Beattie Feb 17 '15 at 16:42

1 Answers1

1

You need to use Marshal.GetDelegateForFunctionPointer and add [MonoNativeFunctionWrapper] to the delegate type:

[MonoNativeFunctionWrapper]
public delegate void ViewWillAppearDelegate (IntPtr self, IntPtr selector);

var del = (ViewWillAppearDelegate) Marshal.GetDelegateForFunctionPointer (original_impl, typeof (ViewWillAppearDelegate));
del (obj.Handle, Selector.GetHandle ("viewWillAppear"));
Rolf Bjarne Kvinge
  • 19,253
  • 2
  • 42
  • 86