.NET Portable Class Library [Serializable] Attribute
Asked Answered
S

1

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?

Stockman answered 25/11, 2014 at 21:34 Comment(2)
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?Cirsoid
The Serialization is done automatically, by the proxy objects. Out of my control.Stockman
I
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.

Inhibitory answered 26/11, 2014 at 6:53 Comment(5)
Worked like a charm. It feels like theres a library to do almost anything for .NETStockman
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.Inhibitory
By the way, there is one limitation with regards to binary serialization and PCL, concerning the [OnSerializing] etc. attributes. Please see this SO question for more information. Hopefully this is not a concern for you.Inhibitory
You're correct, I don't believe this really concerns me here, but thank you for the consideration!Stockman
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?Hollis

© 2022 - 2024 — McMap. All rights reserved.