【问题标题】: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);
      

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

      【讨论】:

        猜你喜欢
        • 2014-08-16
        • 2021-11-21
        • 2020-03-24
        • 1970-01-01
        • 2016-08-17
        • 2013-04-05
        • 1970-01-01
        • 1970-01-01
        • 2017-05-18
        相关资源
        最近更新 更多