3

BinaryFormatter works great, but doesn't exist in Portable class libraries for .NET 4.5.

I've read that it IS in .NET 4.6 Portable. I have not confirmed this because when I change to 4.6 in my project settings, I get a warning message that "4.5 will be automatically targeted" unless I de-select Silverlight, WindowsPhone, Windows Universal, Xamarin, etc), so I can only target .NET 4.6 Portable if I'm NOT targeting additional platforms, thus defeating the purpose.

Here is my original BinarySerializer (Works but NOT PCL because uses BinaryFormatter)

private string BinarySerialize(object Source)
{
    byte[] serializedObject;
    using (MemoryStream stream = new MemoryStream())
    {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, Source);
        serializedObject = stream.ToArray();
    }
    return Convert.ToBase64String(serializedObject);
}

So, that being said, I started trying to re-write my BinarySerializer to use BinaryReader and BinaryWriter which are both available in PCL, and ran into the following issues:

BinaryWriter, can only handle simple types (like string, bool, byte, long, int, etc) and Streams, so think I need to find a way to convert anonymous objects to a Stream.

Am I on the right path? How do you convert types which are not known until runtime (like a System.Delegate - and yes I know serializing delegates produces brittle code) or anonymouse\generic types to their equivalent byte array byte[]? Is there a way to convert an object directly to a Stream or MemoryStream?

If this is not possible at all, how would you recommend I write an interface for my BinarySerializer, so I can implement the functionality in my portable class library while the actual BinaryFormatter classes implementing the interface exists in platform-specific assemblies?

FYI, this is an MVVM project in which "Serializers" are considered Models deriving from a ValueConverter abstract base class. Serialization is only used to transport instantiated tasks to their destination for execution.

turkinator
  • 905
  • 9
  • 25
  • Why are you serializing anyway? And what do you expect serializing a delegate to do for you? Delegates are mostly about _code_, which is not serialized. – D Stanley Mar 30 '16 at 17:32
  • I'm theorizing a rule-builder interface which builds Serializable `Conditions` that can later be deserialized and executed against a dataset. I'm trying to allow custom (typed) rules (any `Func`) as well as allowing `Func` to exist in place of actual `dataTypeT` in the `Condition`'s parameters.Thanks – turkinator Mar 30 '16 at 17:53
  • Well, you can't serialize methods (at least not using the methods you are using now) so I'm not sure that your design will work. – D Stanley Mar 30 '16 at 17:58
  • BinaryFormatter can serialize just about anything, including static Delegates, Actions and Functions, to a byte[]. The only issue is that BinaryFormatter does not exist in Portable .NET 4.5 – turkinator Mar 30 '16 at 18:01
  • 1
    BinaryFormatter is also notorious for creating versioning issues, if you later add new things to the types then the BinaryFormatter class likely won't deserialize your data any more. Binary serialization is meant as a *transport* format, not as a *storage* format. – Lasse V. Karlsen May 04 '16 at 07:07
  • Thanks, @LasseV.Karlsen thats exactly what I need, to use this as a universal transport format in order to ship "task building" commands to their destination (one machine\app\service to another), and execute them under predefined conditions (delegates). – turkinator May 04 '16 at 15:37
  • 1
    `BinaryFormatter` is *not* supported in the Portable Class Library (per [the documentation](https://msdn.microsoft.com/library/system.runtime.serialization.formatters.binary.binaryformatter), which omits any mention) and likely never will be, because it supports the remoting infrastructure, and remoting as a whole is considered deprecated (except across AppDomains, but that's also outside the scope of the PCL). There are some [independent efforts](http://www.codeproject.com/Articles/620154/UniversalSerializer), though. – Jeroen Mostert May 04 '16 at 18:50

0 Answers0