gpt4 book ai didi

c# - 如何调用 method_getImplementation 的 IntPtr 结果

转载 作者:行者123 更新时间:2023-11-29 02:17:46 25 4
gpt4 key购买 nike

调用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] 添加到委托(delegate)类型:

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

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

关于c# - 如何调用 method_getImplementation 的 IntPtr 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28555911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com