我希望你能帮助我。
我正在反序列化一个自定义对象,我收到了这个错误。 该类型位于使用Windows钩子加载的dll中 - 因此,在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)
- 编辑 -
我正在使用命名空间扩展(因此OS挂钩到我的dll)
答案 0 :(得分:2)
现在为时已晚,但我的解决方案如下所示。重写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;
}
...