从 IntPtr 创建 NSException

Create NSException from IntPtr

我指的是这个post:

private static void MyUncaughtExceptionHandler(IntPtr exception)
{
    // this is not working because NSException(IntPtr) is a protected constructor
    var e = new NSException(exception);
    // ...
}

如何在此处创建例外?

我应该这样做吗?

NSException exception = (NSException)ObjCRuntime.Runtime.GetNSObject(handle);

一种选择是创建 NSException 的自定义子类并公开受保护的构造函数。您的 ObjCRuntime.Runtime.GetNSObject 也应该有效(我认为 - 不是 100% 确定)。

您可以像这样创建一个非常简单的子类:

public MyNSException : NSException {
    public MyNSException(IntPtr handle) : base(handle) { }
}

然后您应该可以像这样创建 NSException

var exception = new MyNSException(exception);

我没有尝试使用任何此代码,但这应该可以帮助您进行编译。

您自己找到了正确答案:

NSException exception = (NSException) ObjCRuntime.Runtime.GetNSObject (handle);