c# - Custom Exceptions in PCL files -
i'm converting our .net business objects library pcl file can used xamarin ios/android , while contains poco objects, contains custom exceptions throwing errors.
take 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 pcl doesn't [serializable]
, serializationinfo
. while might away sticking [datacontract] instead of using [serialiable]
, still won't resolve issue serializationinfo
.
is there anyway circumvent problem?
thanks.
update:
i've had @ implementing custom exceptions in portable class library suggested following 2 attributes not recognised:
[classinterfaceattribute(classinterfacetype.none)] [comvisibleattribute(true)]
i must missing reference assembly?
i'm looking @ alternative solution provided in portable class library: recommended replacement [serializable]
hopefully work. update answer once have more info provide.
update:
classinterfaceattribute
part of system.runtime.interoservices cannot add pcl project, @ least it's not visible. missing something?
the other article providing additional info , looks when using conditional compilation, should work, again, while sample code json library appears work, must missing cannot add reference [serializable]
not throw error, don't appear able so.
one thing i've tried comment out:
protected encryptkeynotfoundexception(serializationinfo info, streamingcontext context) : base(info, context) { }
and can compile pcl project ok, question need this?
thanks.
i think have misinterpreted answer in suggested link. don't need add classinterfaceattribute
or comvisibleattribute
in custom exception implementation. if in exception class .net framework see :
[serializableattribute] [classinterfaceattribute(classinterfacetype.none)] [comvisibleattribute(true)] public class exception : iserializable, _exception
and @ exception class silverlight, this
[classinterfaceattribute(classinterfacetype.none)] [comvisibleattribute(true)] public class exception
serializableattribute
not available.
another diference exception class silverlight has 3 constructors. constructor exception(serializationinfo, streamingcontext)
not available. , can see in below screenshot of custom exception implementation in pcl library 3 constructors available exception. there no such constructor available trying create :
encryptkeynotfoundexception(serializationinfo info, streamingcontext context) : base(info, context) { }
so, in pcl custom exception implementation datacontract instead of serializable, :
[datacontract] public class encryptkeynotfoundexception : system.exception { public encryptkeynotfoundexception() : base() { } public encryptkeynotfoundexception(string message) : base(message) { } public encryptkeynotfoundexception(string message, exception innerexception) : base(message, innerexception) { } }
Comments
Post a Comment