views:

54

answers:

5

Supose I serialized a third party library type object with BinaryFormatter. An assemby that does not references this library tries to deserialize the bytes. Will it work?

I do not expect to it be cast to the correct type, I just want to retrieve it as an object instance so I can group it and serialize it again.

+1  A: 

No; if the type itself isn't referenced (in some way by some loaded assembly), then it can't be instantiated. Even if you don't need (or want) to reference the instance in a strongly-typed way, the object itself must still be an instance of that type.

If the assembly is available (and discoverable), then it will be loaded, but in a strict sense no, you won't be able to deserialize a type from a completely unreferenced assembly.

Adam Robinson
A: 

No, it won't work, in order to deserialize a object you need to reference the assembly where the object is defined.

AlbertEin
+2  A: 

This should work fine, but only if that library is deployed with the app. You don't need a direct reference to it.

During serialization, the BinaryFormatter stores the full assembly name (display name, public key token, version number) along with the type information. This is enough information for the deserializer to go off and load that assembly again. Note that path information is not stored, hence the need to deploy the assembly in the app that does the deserialization.

Tim Robinson
What's with the downvote? Is this information wrong?
Tim Robinson
Two Robinsons in the same thread =)
Jader Dias
+1  A: 

If you are only trying to temporarily obtain the serialized information so that you can group it, can you just read the raw bytes from the serialized stream and group them? Maybe into a List<byte[]> instance? This assumes that the final destination can make some assumptions about the information represented by each byte array.

Dave
Thanks for the suggestion
Jader Dias
A: 

Yes, if you create a serialization binder, you can deserialize the type to a different type. But you won't be able to instantiate an instance of the original type without it's definition (to which you'd be required to have the assembly on hand)

MaLio