9

I'm currently converting our .net business objects library to a PCL file so that it can be used with Xamarin IOS/Android and while it contains mainly POCO objects, it also contains custom exceptions but this is throwing errors.

Take a typical Custom Exception:

[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) { }
}

As expected the PCL doesn't like [Serializable] and SerializationInfo. While I might get away with sticking [DataContract] instead of using [Serialiable], it still won't resolve the issue with SerializationInfo.

Is there anyway to circumvent this problem?

Thanks.

Update:

I've had a look at Implementing custom exceptions in a Portable Class Library as suggested but the following 2 attributes are not recognised:

[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]

I must be missing a reference but to which assembly?

I'm currently looking at an alternative solution as provided in Portable class library: recommended replacement for [Serializable]

Hopefully this will work. I will update my answer once I have more info to provide.

Update:

ClassInterfaceAttribute is part of the System.RunTime.InteroServices but I cannot add this to my PCL project, well at least it's not visible. Am I missing something?

The other article is providing additional info and it looks that when using conditional compilation, this should work, but again, while the sample code from the json library appears to work, I must be missing something as I cannot add a reference so that [Serializable] does not throw an error, but I don't appear to be able to do so.

One thing I've tried is to simply comment out:

protected EncryptKeyNotFoundException(SerializationInfo info, 
StreamingContext context) : base(info, context) { }

And I can compile my pcl project ok, so the question is do I need this?

Thanks.

Community
  • 1
  • 1
Thierry
  • 6,142
  • 13
  • 66
  • 117
  • 2
    I think this will be helpful for you [Implementing custom exceptions in a Portable Class Library](http://stackoverflow.com/questions/13604713/implementing-custom-exceptions-in-a-portable-class-library) – Mukesh Modhvadiya Aug 19 '15 at 13:16
  • I had found this article but didn't think it was relevant. I'll give it a shot and update when done. Thanks. – Thierry Aug 20 '15 at 11:31
  • I've updated my answer but unfortunately, it's not helping. Am I missing something? – Thierry Aug 26 '15 at 16:55
  • From your update looks like you have misunderstood the suggested answer. I can not add much explanation in comment, so adding in the answer. – Mukesh Modhvadiya Aug 28 '15 at 12:58

1 Answers1

1

I think you have misinterpreted the answer in suggested link. You don't need to add ClassInterfaceAttribute or ComVisibleAttribute in your custom exception implementation. If we look in Exception class for .NET Framework we see :

[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public class Exception : ISerializable, _Exception

and at Exception class for Silverlight, this

[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public class Exception

SerializableAttribute is not available.

Another diference is that Exception class for Silverlight has only 3 constructors. Constructor Exception(SerializationInfo, StreamingContext) is not available. And also we can see in below screenshot of custom exception implementation in PCL library that only 3 constructors available for Exception. There is no such constructor available which you are trying to create :

EncryptKeyNotFoundException(SerializationInfo info, StreamingContext context)
   : base(info, context) { }

enter image description here

So, in PCL custom exception implementation with DataContract instead of Serializable, would be something like this :

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

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

    public EncryptKeyNotFoundException(string message, Exception innerException) : base(message, innerException) { }
}
Mukesh Modhvadiya
  • 2,178
  • 2
  • 27
  • 32
  • Hi Mak, thanks for the detailed clarification. If you look at my updated question, this is what I ended up doing i.e. getting rid of the overloaded method that used the SerializationInfo. – Thierry Aug 28 '15 at 13:09
  • Yes, you were ended up with working solution, but I thought this would be helpful to understand why it is so. :) – Mukesh Modhvadiya Aug 28 '15 at 13:33
  • 1
    Totally agree with you & thanks for that! Learn something new today! – Thierry Aug 29 '15 at 00:14