6

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Adam Schiavone
  • 2,412
  • 3
  • 32
  • 65
  • Do you really need to serialize them as a binary object? Would converting them to JSON or XML work for your purposes so you can avoid this problem? – mason Nov 25 '14 at 21:50
  • The Serialization is done automatically, by the proxy objects. Out of my control. – Adam Schiavone Nov 25 '14 at 21:57

1 Answers1

10

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.

Community
  • 1
  • 1
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
  • 1
    Worked like a charm. It feels like theres a library to do almost anything for .NET – Adam Schiavone Nov 26 '14 at 13:58
  • In principle, yes, the library could be very much extended to cover a larger part of .NET. When I developed this library, I however concentrated on my own needs to facilitate PCL:ification of *AForge.NET*, *Accord.NET*, *Encog* and *fo-dicom*. But I happily encourage all user contributions that can help to extend the library further. – Anders Gustafsson Nov 26 '14 at 14:03
  • By the way, there is one limitation with regards to binary serialization and PCL, concerning the `[OnSerializing]` etc. attributes. Please see this SO [question](http://stackoverflow.com/q/20666731/650012) for more information. Hopefully this is not a concern for you. – Anders Gustafsson Nov 26 '14 at 14:11
  • You're correct, I don't believe this really concerns me here, but thank you for the consideration! – Adam Schiavone Nov 26 '14 at 14:54
  • I get System.PlatformNotSupportedException: 'PCL' when I call stuff in System.IO, how do I use it? Edited: Aha okey, it is a dummy implementations of legancy code? – Alexander Jan 30 '18 at 16:19