As consequence of my previous question ( XML serialization of interfaces ) I obtained another problem...
I have an application that export data from a database. The export procedure is implemented by different concrete classes that implement a common interface used for invocation.
The concrete implementations are loaded as plug-ins (DLLs) so I don't reference them in my code directly.
I need to serialize instances of these concrete classes as byte arrays into my database, but now when I try to deserialize them from a byte array I obtain a SerializationException: Unable to find assembly …
I suppose it appens because I load at runtime the dll with the concrete implementation of my interface...
How can I solve it?
NOTE I'm using this code to deserialize objects:
public static object DeSerialize(byte[] arrayToDeSerialize)
{
object serializedObject;
using (MemoryStream stream = new MemoryStream(arrayToDeSerialize))
{
//Creating binary formatter to De-Serialize string.
BinaryFormatter formatter = new BinaryFormatter();
//De-Serializing.
serializedObject = formatter.Deserialize(stream);
}
return serializedObject;
}
You could hook the AppDomain.AssemblyResolve event to load the assemblies as they are needed. the event is raised each time that the runtime needs an assembly that it cannot resolve. It gives you one last chance to provide the assembly before the "Unable to find assembly" exception is thrown. Examples are on the page that I linked.