从 IntPtr 创建 NSException

Posted

技术标签:

【中文标题】从 IntPtr 创建 NSException【英文标题】:Create NSException from IntPtr 【发布时间】:2016-05-30 12:17:49 【问题描述】:

我指的是这个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);

【问题讨论】:

【参考方案1】:

你自己找到了正确答案:

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

【讨论】:

【参考方案2】:

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

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

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

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

var exception = new MyNSException(exception);

我没有尝试过使用这些代码,但这应该可以让你编译。

【讨论】:

以上是关于从 IntPtr 创建 NSException的主要内容,如果未能解决你的问题,请参考以下文章