如何调用 method_getImplementation 的 IntPtr 结果

How to Invoke the IntPtr result of method_getImplementation

调用 method_getImplementation(method) 的结果是 IntPtr。如何将 IntPtr 转换为方法?

[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);
}

在 obj-c 中:

IMP originalImp = method_setImplementation(method,swizzleImp);
originalImp(self,_cmd)

您需要使用Marshal.GetDelegateForFunctionPointer并在委托类型中添加[MonoNativeFunctionWrapper]

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

var del = (ViewWillAppearDelegate) Marshal.GetDelegateForFunctionPointer (original_impl, typeof (ViewWillAppearDelegate));
del (obj.Handle, Selector.GetHandle ("viewWillAppear"));