从IntPtr创buildNSException

我指的是这个post :

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

我如何在这里创build一个exception?

我应该这样做吗?

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

你自己find了正确的答案:

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

一个select是创build一个NSException的自定义子类,并公开受保护的构造函数。 你ObjCRuntime.Runtime.GetNSObject也应该工作(我认为 – 不是100%肯定)。

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

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

然后,你应该能够像这样创build你的NSException

 var exception = new MyNSException(exception); 

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