反序列化BinaryFormatter给出SerializationException [英] BinaryFormatter deserialize gives SerializationException

查看:413
本文介绍了反序列化BinaryFormatter给出SerializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个:


System.Runtime.Serialization.SerializationException:无法找到
程序集'myNameSpace,版本= 1.0.0.0,区域性=中性,
PublicKeyToken = null

System.Runtime.Serialization.SerializationException: Unable to find assembly 'myNameSpace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

当试图反序列化另一个程序中的某些数据时,

When trying to deserialize some data in another program than the program I serialized it with.

经过一番谷歌搜索后,我发现这显然只能使用共享程序集完成。

After some googling I've found out that apparently this can only be done using a shared assembly.

但是,我的数据库中充满了这个序列化的对象,我需要一个实用程序才能将它们释放出来。有没有办法重写此行为,而只将其完全喂给同一个类并强制它反序列化?

However, my database is full with this serialized objects, and I need a utility program to get them out. Is there a way to override this behavior and just feed it the exact same class and force it do deserialize?

我已经找到了该代码段,但我不知道应在何处以及如何放置/使用它。

I already found this snippet, but I don't understand how and where I should put/use this.

   static constructor() {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
   }

    static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
        Assembly ayResult = null;
        string sShortAssemblyName = args.Name.Split(',')[0];
         Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();
         foreach (Assembly ayAssembly in ayAssemblies) {
            if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0]) {
                 ayResult = ayAssembly;
                 break;
            }
         }
         return ayResult;
    }


推荐答案

您需要提供一个引用原始类型某种方式,以便实用程序知道如何反序列化。

You will need to provide a reference to the original type somehow so that the utility program knows how to deserialize it.

简单的方法是将DLL添加类型最初是在对实用程序项目的引用中定义的。

The easy way is just to add the DLL the types were originally defined in as a reference to the utility project.

您发布的代码使您可以在反序列化程序确定找不到类型时动态加载相同的DLL。 。这是一个更困难的方法(但不是那么困难),但是在两种情况下,您都将需要一个DLL来定义类型...因此,最简单的方法就是添加引用来静态链接。

The code you posted allows you to dynamically load that same DLL when the deserializer determines it can't find the type. This is a more difficult approach (but not that difficult), but in both cases you will need a DLL that defines the types... so probably easiest just to statically link by adding the reference.

如果您的类型当前不在DLL中(例如,如果它们在EXE中),建议您将类从EXE中拉出,并放入新的DLL中,并引用该类来自原始项目和util项目的DLL。

If your types are not currently in a DLL (e.g. if they are in an EXE), I suggest you pull the classes out of the EXE into a new DLL, and reference that DLL both from the original project and from the util project.

这篇关于反序列化BinaryFormatter给出SerializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆