gpt4 book ai didi

c# - 无法使用 BinaryFormatter.Deserialize 找到程序集

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:54 25 4
gpt4 key购买 nike

我希望你能帮助我。

我正在反序列化自定义对象,但出现此错误。该类型位于一个 dll 中,该 dll 使用 Windows Hook 加载 - 因此,在 explorer.exe 下运行

我明白为什么会出现这个错误,这是因为 DLL 不驻留在“explorer.exe”进程中。有两种解决方案:1.在GAC中安装程序集2. 使用二进制格式化程序的“Binder”。

我不想使用其中任何一个,因为 DLL 实际上已加载到 explorer.exe 中(当我附加时,我看到 DLL 确实已加载)。

我正在序列化一个自定义对象 - 在执行反序列化的同一个 DLL 中创建

这是我的代码:

     BinaryFormatter binaryFormatter = new BinaryFormatter();
byte[] serializedObject;


using (MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, new MyCustomObject());
serializedObject = memoryStream.ToArray();
}
BinaryFormatter binaryFormatter2 = new BinaryFormatter();
object deserializedObject;
using (MemoryStream memoryStream2 = new MemoryStream(serializedObject))
{
deserializedObject = binaryFormatte2r.Deserialize(memoryStream2); // Unable to find this assembly
}

顺便说一句。我查看了二进制格式化程序代码,不知何故它确实尝试加载 DLL.. 但它已经加载到进程中..

错误:

Unable to find assembly 'AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=Key'.

stack trace:
at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
at System.Runtime.Serialization.Formatters.Binary.BinaryConverter.TypeFromInfo(BinaryTypeEnum binaryTypeEnum, Object typeInformation, ObjectReader objectReader, BinaryAssemblyInfo assemblyInfo, InternalPrimitiveTypeE& primitiveTypeEnum, String& typeString, Type& type, Boolean& isVariant)
at
System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadArray(BinaryHeaderEnum binaryHeaderEnum)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)

-- 编辑--

我正在使用命名空间扩展(因此操作系统 Hook 到我的 dll)

最佳答案

为时已晚,但我的解决方案如下所示。 BindToType 的实现被覆盖以解决您的问题。

[Serializable]
public class YourClass : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
Type tyType = null;
string sShortAssemblyName = assemblyName.Split(',')[0];

Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();

foreach (Assembly ayAssembly in ayAssemblies)
{
if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0])
{
tyType = ayAssembly.GetType(typeName);
break;
}
}
return tyType;
}
...

This link helped me

关于c# - 无法使用 BinaryFormatter.Deserialize 找到程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24673376/

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