开发者

Problems with .net binary seralization, where an interface is shared, but not the actual classes

开发者 https://www.devze.com 2023-03-17 06:30 出处:网络
Okay my particular situation is fairly unusal - I have two applications. The main one saves and loads objects to a file using standard .NET binary serialization. The second one loads from the same fil

Okay my particular situation is fairly unusal - I have two applications. The main one saves and loads objects to a file using standard .NET binary serialization. The second one loads from the same file, however it simply does not need half the saved classes - it just wants to extract the classes it wants.

The problem came because I wanted to not include the same classes in both applications, so I separated out the bits I need开发者_StackOverflow社区ed with and simply made sure the interface was in a DLL common to both applications.

All was good until I needed to actually deserialise in the second application the binary deserialiser did not recognise the interface. (complaining that it was unable to find the assembly)


I found part of my answer - "AllowAllAssemblyVersionsDeserializationBinder"... This is a seriously powerful way to intercept Binary Serialization, and allowed me to "type swap" the original class with it's interface. This allowed the deserialization to continue (i.e. load my file) - then through away the unwanted objects. This also allows me to deal more powerfully with version issues between different files.

There is a good run through here: http://spazzarama.wordpress.com/2009/06/25/binary-deserialize-unable-to-find-assembly/

So I am mentioning it here because others might find it useful, plus I am SURE that something is wrong when an obscure search for "AllowAllAssemblyVersionsDeserializationBinder" comes up with no answers on StackOverflow - it is exactly the type of question / answer that needs to be mentioned :)

[Note I mention it solves my problem in part - the one class I do want to "share", but only need the interface values not the full class, still does not deserialise the actual values - so still need to investigate whether I have to use abstract classes, etc.]

[Note 2: I've used binary serialisation as a quick fix to speed up development whilst I sort out the full design, so I hope that the main design work progresses and I don't have to rely on the binary serialiser in the first place...]

0

精彩评论

暂无评论...
验证码 换一张
取 消