gpt4 book ai didi

c# - 跨程序集移动的反序列化类型

转载 作者:行者123 更新时间:2023-11-30 17:56:24 26 4
gpt4 key购买 nike

我有一堆类型已从一个程序集移动到另一个程序集。我正在尝试使用 SerializationBinder 将使用旧程序集序列化的数据反序列化 为新程序集中的类型。

编辑:程序集的根 namespace 与程序集名称相同。旧程序集已不存在。

sealed class TypeMapBinder : SerializationBinder
{
public override Type BindToType( string assemblyName, string typeName )
{
Type typeToDeserialize = null;

if ( assemblyName.Contains( "old namespace" ) )
typeToDeserialize = Type.GetType( typeName.Replace( "old namespace", "new namespace" ) );
else
typeToDeserialize = Type.GetType( String.Concat( typeName, ", ", assemblyName ) );

return typeToDeserialize;
}
}

反序列化代码看起来像这样 -

using ( MemoryStream ms = new MemoryStream( byteArr ) )             {
BinaryFormatter formatter = new BinaryFormatter( );
formatter.Binder = new TypeMapBinder( );
return formatter.Deserialize( ms );
}

当我尝试反序列化时,我在尝试加载旧程序集时遇到错误。

Could not load file or assembly 'old assembly' or one of its dependencies. The system cannot find the file specified.

最佳答案

我想我遇到了同样的问题。

我的 SerializationBinder 的 BindToType 方法没有发出任何引用旧程序集的类型,但是 BinaryFormatter 仍然尝试加载那个旧程序集:

System.IO.FileNotFoundException : Could not load file or assembly 'Old.Interfaces, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, ref StackCrawlMark stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, ref StackCrawlMark stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, ref StackCrawlMark stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, ref StackCrawlMark stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
at System.UnitySerializationHolder.GetRealObject(StreamingContext context)
at System.Runtime.Serialization.ObjectManager.ResolveObjectReference(ObjectHolder holder)
at System.Runtime.Serialization.ObjectManager.DoFixups()
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)
[...]

如果我为 AppDomain.CurrentDomain.AssemblyResolve 添加一个处理程序来加载 New.Interfaces 而不是 Old.Interfaces 它会抛出另一个异常:

System.TypeLoadException : Could not load type 'Old.Interfaces.MyClass' from assembly 'New.Interfaces, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)
at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at System.UnitySerializationHolder.GetRealObject(StreamingContext context)
at System.Runtime.Serialization.ObjectManager.ResolveObjectReference(ObjectHolder holder)
at System.Runtime.Serialization.ObjectManager.DoFixups()

但是,已经为 Old.Interfaces.MyClass 类型调用了 BindToType 方法,正如我所说,在 BindToType 中我没有返回可能引用旧类的单一类型.

此外,如果我更改反序列化的二进制数据,以便字符串 Old 的出现被 New 替换,对象图将最终加载。我对这个解决方案不是很满意。

关于c# - 跨程序集移动的反序列化类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14217802/

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