PCL文件中的自定义例外

Thi*_*rry 9 c# xamarin.ios xamarin.android portable-class-library xamarin

我正在将我们的.net业务对象库转换为PCL文件,以便它可以与Xamarin IOS/Android一起使用,虽然它主要包含POCO对象,但它也包含自定义异常,但这会引发错误.

采取典型的自定义异常:

[Serializable]
public class EncryptKeyNotFoundException : Exception
{
    public EncryptKeyNotFoundException()
        : base() { }

    public EncryptKeyNotFoundException(string message)
        : base(message) { }

    public EncryptKeyNotFoundException(string format, params object[] args)
        : base(string.Format(format, args)) { }

    public EncryptKeyNotFoundException(string message, Exception innerException)
        : base(message, innerException) { }

    public EncryptKeyNotFoundException(string format, Exception innerException, params object[] args)
        : base(string.Format(format, args), innerException) { }

    protected EncryptKeyNotFoundException(SerializationInfo info, StreamingContext context)
        : base(info, context) { }
}
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,PCL不喜欢[Serializable]和SerializationInfo.虽然我可能会坚持使用[DataContract]而不是使用[Serialiable],但它仍然无法解决问题SerializationInfo.

反正有没有绕过这个问题?

谢谢.

更新:

我已经看过按照建议在可移植类库中实现自定义异常但是无法识别以下2个属性:

[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
Run Code Online (Sandbox Code Playgroud)

我必须错过一个参考但是哪个汇编?

我目前正在寻找Portable类库中提供的替代解决方案:推荐替代[Serializable]

希望这会奏效.一旦我有更多信息要提供,我会更新我的答案.

更新:

ClassInterfaceAttribute是System.RunTime.InteroServices的一部分,但我无法将其添加到我的PCL项目中,至少它是不可见的.我错过了什么吗?

另一篇文章提供了额外的信息,它看起来在使用条件编译时,这应该可行,但同样,当json库中的示例代码似乎工作时,我必须遗漏一些东西,因为我无法添加引用,所以这[Serializable]不抛出一个错误,但我似乎无法这样做.

我尝试过的一件事就是简单地说出来:

protected EncryptKeyNotFoundException(SerializationInfo info, 
StreamingContext context) : base(info, context) { }
Run Code Online (Sandbox Code Playgroud)

我可以编译我的pcl项目,所以问题是我需要这个吗?

谢谢.

Muk*_*iya 1

我认为您误解了建议链接中的答案。您不需要在自定义异常实现中添加ClassInterfaceAttribute或。ComVisibleAttribute如果我们查看.NET Framework 的 Exception 类,我们会看到:

[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public class Exception : ISerializable, _Exception
Run Code Online (Sandbox Code Playgroud)

在Silverlight 的 Exception 类中,这

[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public class Exception
Run Code Online (Sandbox Code Playgroud)

SerializableAttribute不可用。

另一个区别是 Silverlight 的 Exception 类只有 3 个构造函数。构造函数Exception(SerializationInfo, StreamingContext)不可用。我们还可以在下面的 PCL 库中自定义异常实现的屏幕截图中看到,只有 3 个构造函数可用于异常。没有您要创建的可用构造函数:

EncryptKeyNotFoundException(SerializationInfo info, StreamingContext context)
   : base(info, context) { }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

因此,在使用 DataContract 而不是 Serialized 的 PCL 自定义异常实现中,将是这样的:

[DataContract]
public class EncryptKeyNotFoundException : System.Exception
{
    public EncryptKeyNotFoundException() : base() { }

    public EncryptKeyNotFoundException(string message) : base(message) { }

    public EncryptKeyNotFoundException(string message, Exception innerException) : base(message, innerException) { }
}
Run Code Online (Sandbox Code Playgroud)