I'm serializing some data like fields and custom class to create a binary data (byte array).

Then I want to Deserialize it back from binary data to fields and class.

But I get an exception. It would all work fine if these two methods would happen in same assembly - but its not.

I do Serialization in one assambly, and do the Deserialization in another one. And this is the excaption saying too:
Unable to find assembly 'MyAssamblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

NOTE 1: I have no issues with getting the fields back, only the classes causes them.

K... Context; should we assume you are using BinaryFormatter? The type you serialized: is that in an assembly that both projects are referencing? Are you accidentally serialising more data than you expect via events? (that happens really often)
–
Marc Gravell♦Oct 26 '12 at 16:42

2 Answers
2

No you don't. At least, not as far as the runtime is concerned. You have two different types that happen to have the same name. A type is defined by its assembly. Thus "SomeType in AssemblyA" is completely different to "SomeType in AssemblyB", even if they happen to have been compiled from the same source file.

BinaryFormatter works with type information, so this won't work. One option would be to move the type to a library dll that both the other projects reference - then it is only defined once, and it will be happy.

Another option is to work with a contract-based serializer (rather than a type-based serializer). This means that "classes that look similar enough" are fine, even if they are in different assemblies (and perhaps have different source, as long as it is "similar enough"). Examples of suitable serializers for this would include (plus a few others) XmlSerializer, DataContractSerializer (but not NetDataContractSerializer), JavaScriptSerializer, or protobuf-net if you want dense raw binary.

Yes, I use BinaryFormatter. I would like to go with your 1st option. So, I have to create a new project, and add them to both of the other two projcts? Am I right?
–
Mitja BoncaOct 26 '12 at 16:51

1

In the solution, add a new project - select "library". Now put the class there and remote it from the other two places. Finally, for both the original projects: references, add reference... select the new library project you just created.
–
Marc Gravell♦Oct 26 '12 at 16:56

This is what I just did! Thxa lot mate. Will let you know if it will work (but I know it will :)).
–
Mitja BoncaOct 26 '12 at 17:00

Works, but I got null for the class data. But this is now my issue to resalve it. Thx ones again.
–
Mitja BoncaOct 26 '12 at 17:06

I did managed to put it all into work now, many thx :)
–
Mitja BoncaOct 26 '12 at 17:19

All the assemblies containing classes in the class hierarchy of the object you are deserializing must be present in the application in which you are performing this deserialization. They could be either explicitly referenced (if you need compile-time safety with those classes) or only placed in the bin folder of the application so that they could be resolved at runtime. If they are not explicitly referenced you will have to use reflection in order to read the values from the deserialized instance.