I have a common class library containing many models for my server and my client. Since the client is running under Xamarin, the common library must be a Portable Class Library (PCL).
In my server, these objects are passed around with AppDomain Remoting/Marshaling, so to my understanding an object either needs to be marked as [Serializable]
or inherit from MarshalByRefObject
Being in a PCL, I cannot do either of these things to any of my models.
My question is: How can I make these objects work with AppDomain Remoting/Marshaling and let them reside in a Portable Class Library?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With