BinaryFormatter.反序列化“无法找到程序集"在ILMerge之后 [英] BinaryFormatter.Deserialize "unable to find assembly" after ILMerge

查看:169
本文介绍了BinaryFormatter.反序列化“无法找到程序集"在ILMerge之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有引用的dll的C#解决方案(也是具有相同.Net版本的C#).当我构建解决方案并运行生成的exe而不合并exe和所引用的dll时,一切正常.

I have a C# solution with a referenced dll (also C# with the same .Net version). When I build the solution and run the resulting exe, without merging the exe and the referenced dll, everything works fine.

现在,我想将这些合并为一个exe.我运行ILMerge,一切似乎都正常.我尝试执行该exe文件,在它试图反序列化引用的dll中定义的对象之前,它似乎运行得很好.

Now I want to merge these into one exe. I run ILMerge and everything appears to work ok. I try to execute the exe and it seems to run just fine until it tries to deserialize an object defined in the referenced dll.

using (Stream fstream = new FileStream(file_path, FileMode.Open))
{
    BinaryFormatter bf = new BinaryFormatter();
    return bf.Deserialize(fstream) as ControlledRuleCollection; 
    // throws unable to find assembly exception
}

这里可能缺少一些ILMerge选项吗?

Is there maybe some ILMerge option I'm missing here?

推荐答案

听起来您已经在DLL中序列化了一个对象,然后将所有程序集与ILMerge合并,现在正尝试对该对象进行反序列化.这根本行不通.二进制序列化的反序列化过程将尝试从原始DLL加载对象的类型.此DLL在ILMerge之后不存在,因此反序列化将失败.

It sounds like you've serialized an object inside a DLL, then merged all of the assemblies with ILMerge and are now trying to deserialize that object. This simply won't work. The deserialization process for binary serialization will attempt to load the object's type from the original DLL. This DLL doesn't exist post ILMerge and hence the deserialization will fail.

序列化和反序列化过程都需要在合并之前或之后进行操作.不能混在一起

The serialization and deserialization process need to both operate pre or post merge. It can't be mixed

这篇关于BinaryFormatter.反序列化“无法找到程序集"在ILMerge之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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