2013-01-08 35 views
3

我有一堆從一個程序集移到另一個程序集的類型。我試圖通過使用SerializationBinder反序列化數據使用舊程序集序列化爲新程序集中的類型。反序列化在程序集中移動的類型

編輯:程序集的根名稱空間與程序集名稱相同。舊的組件不再存在。

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);    
} 

當我嘗試反序列化,我得到的地方嘗試加載舊組裝誤差。

無法加載文件或程序集「舊程序集」或其某個 依賴項。該系統找不到指定的文件。

+0

您的BindToType覆蓋是否被調用?它是否構成了正確的類型名稱? –

+0

yes沒有問題,typeToDeserialize從不爲null –

+0

尋找「舊名稱空間」和AssemblyName之間的匹配是沒有意義的。顯然這需要是「舊程序集名稱」。你對這段代碼進行了模糊處理,以至於它不再可以診斷出它可能有什麼問題。 –

回答

1

我想我看到了同樣的問題。

我SerializationBinder的BindToType方法不排放任何類型的,它指的是舊的組裝,但儘管如此,BinaryFormatter的嘗試加載舊的組件:如果我的AppDomain.CurrentDomain.AssemblyResolve添加一個處理程序

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) 
    [...] 

負載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() 

然而,BindToType方法已經呼籲類型Old.Interfaces.MyClass和我說,我BindToType沒有返回可能引用舊類的單一類型。此外,如果我更改了獲取反序列化的二進制數據,以便字符串Old的出現被替換爲New,對象圖最終加載。我對這個解決方案並不滿意。

相關問題