Supose I serialized a third party library type object with BinaryFormatter
. An assemby that does not references this library tries to de开发者_开发问答serialize 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.
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.
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.
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.
No, it won't work, in order to deserialize a object you need to reference the assembly where the object is defined.
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)
精彩评论