gpt4 book ai didi

c# - 系统.Runtime.Serialization.SerializationException : Unable to find assembly MyAssembly

转载 作者:太空狗 更新时间:2023-10-29 19:59:57 25 4
gpt4 key购买 nike

所以我找到了一堆关于这个主题的话题,但我认为我还没有找到一个适用的话题。

基本上,我的 .exe 加载一个 .dll (MyAssembly) 文件,该文件执行序列化和加载。显然它序列化得很好。

但是当我去反序列化 MyAssembly.dll 文件中的文件时,它会爆炸并出现本文标题中的错误。

有人有什么想法吗?我不明白它怎么找不到调用代码的程序集!

我的代码:

// deserialize


using (var target = new System.IO.FileStream(Path, System.IO.FileMode.OpenOrCreate))
{
var bin = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
var Obj = bin.Deserialize(target);
if (Obj != null)
{
ObjectToStore = (ObjectTypeInMyAssembly)Obj;
}
}

// serialize
using (var target = new System.IO.FileStream(Path, System.IO.FileMode.OpenOrCreate))
{
var bin = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bin.Serialize(target, ObjectToStore);
}

最佳答案

DLL和EXE在同一个文件夹吗?
我看到您序列化/反序列化了一个存在于 DLL(“MyAssembly”)中的对象。反序列化时,格式化程序从序列化数据中确定类型的名称,并尝试在主要可执行文件夹(即- EXE 文件夹)的程序集中找到该类型。
解决方案- 将 DLL 移动到 EXE 文件夹。有一种方法可以使格式化程序在另一个程序集中搜索,捕获事件 AppDomain.AssemblyResolve 并返回您的 DLL。参见 MSDN .

关于c# - 系统.Runtime.Serialization.SerializationException : Unable to find assembly MyAssembly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14990980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com