序列化/反序列化从动态加载的dll创建的对象

时间:2014-03-02 13:00:20

标签: c# serialization dll deserialization

我动态加载一个dll,创建一个新对象并将该对象序列化为字节数组。它工作正常。但是我无法反序列化此对象,因为它抛出异常SerializationException:无法找到程序集'TicTacToe,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'。

这是短代码expamle

        var loadedGame = Assembly.LoadFrom(pathdll);

        var instances = from t in loadedGame.GetTypes()
                        where t.GetInterfaces().Contains(typeof(IMove))
                        select Activator.CreateInstance(t, 15) as IMove;

        IMove move = instances.First();

        var formatter = new BinaryFormatter();

        using (MemoryStream stream = new MemoryStream())
        {
            formatter.Serialize(stream, move);
            stream.Position = 0;
            formatter.Deserialize(stream); // exception is thrown here

        }

你知道如何反序列化对象吗?

更新:

解决方案: How to serialize/deserialize an object loaded from another assembly?

谢谢你,韦斯顿!

1 个答案:

答案 0 :(得分:0)