SerializationException for dynamically loaded Type

眉间皱痕 提交于 2019-12-11 23:34:11

问题


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;
    }

回答1:


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.



来源:https://stackoverflow.com/questions/9162279/serializationexception-for-dynamically-loaded-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!