【问题标题】:Create NSException from IntPtr从 IntPtr 创建 NSException
【发布时间】: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);
【问题讨论】:
标签:
c#
ios
xamarin.ios
nsexception
【解决方案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);
我没有尝试过使用这些代码,但这应该可以让你编译。