I have created a PCL support library called CSShim that contains a "mock" [Serializable]
attribute. If this library is referenced from your PCL library, you can use [Serializable]
in your code.
Then, when you consume your PCL library in a regular .NET desktop application, the reference to the PCL CSShim is replaced with a reference to the .NET anolugue of CSShim, using the so-called "bait-and-switch" technique. The .NET analogue forwards the invocation of [Serializable]
to the .NET implementation in mscorlib using [TypeForwardedTo]
.
CSShim is currently available from NuGet for PCL profile 259, targeting .NET Framework 4.5 and higher, Windows 8 and higher, Windows Phone 8.1, Windows Phone Silverlight 8 and higher, Xamarin Android and Xamarin iOS.
The CSShim source code is available on Github. If it is a limitation that the PCL library only targets .NET 4.5 and higher, you could theoretically re-target the PCL library to a .NET Framework 4 profile such as profile 328, although re-targeting may be "a rough ride" :-)
Alternatively, you could create your own PCL support library containing only "mock" implementations of the types related to SerializableAttribute
, and create a .NET analogue of the support library using type forwarding to invoke the valid types in the .NET core assemblies. I have outlined this approach in more detail in this StackOverflow answer.