我试图将这个Objective-c代码移植到c#但没有成功......
IMP imp = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
((void (*)(id, SEL, void*, BOOL, BOOL, id))originalImp)(me, sel, arg0, TRUE, arg2, arg3);
我已经转换成了这个:
static Selector selector1;
[MonoNativeFunctionWrapper]
public delegate void OriginalDelegate(NSObject me, Selector sel, IntPtr arg0, bool arg1, bool arg2, NSObject arg3);
delegate void CaptureDelegate(NSObject me, IntPtr arg0, bool arg1, bool arg2, NSObject arg3);
[MonoPInvokeCallback(typeof(CaptureDelegate))]
static void MyCapture(NSObject me, IntPtr arg0, bool arg1, bool arg2, NSObject arg3)
{
OriginalDelegate del = (OriginalDelegate)Marshal.GetDelegateForFunctionPointer(_original, typeof(OriginalDelegate));
del(me, selector1, arg0, true, arg2, arg3);
}
代码编译正确。问题非常局限于:
(我,selector1,arg0,true,arg2,arg3),特别是在selector1参数中。此调用执行委托OriginalDelegate。 运行时发生的错误是:
Type ObjCRuntime.Selector which is passed to unmanaged code must have a StructLayout attribute.
它告诉我Selector对象必须定义StructAttribute属性。 ????
有人能帮我们一把吗?
谢谢。
编辑:我已经添加了Selector1声明,如果我尝试更改类型Selector to IntPtr
,那么我发现以下错误:
Objective-C exception thrown.Name: NSInvalidArgumentException Reason: -_NSMallocBlock__ _stopAssistingNode]: unrecognized selector sent to instance 0x12ff844d0
Native stack trace:
0 CoreFoundation 0x000000011465e12b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001153fcf41 objc_exception_throw + 48
2 CoreFoundation 0x00000001146df024 -[NSObject(NSObject) doesNotRecognizeSelector:]*- + 132
3 CoreFoundation 0x00000001145e0f78 ___forwarding___ + 1432
4 CoreFoundation 0x00000001145e0958 _CF_forwarding_prep_0 + 120
5 WebKit 0x00000001072de47e -[WKContentView(WKInteraction) _startAssistingNode:userIsInteracting:blurPreviousNode:userObject:] + 291
6 ??? 0x000000013562b176 0x0 + 5190627702
再次感谢!