From 714d8c7092857db499beb3ba674293dfb8cd05b4 Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Mon, 5 Jan 2015 18:18:29 +0800 Subject: [PATCH] Fix a issue when deserialize binary storage. --- Wox.Infrastructure/Storage/BinaryStorage.cs | 36 ++++++++++++++++++--- Wox/Themes/ThemeBuilder/Template.xaml | 2 +- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Wox.Infrastructure/Storage/BinaryStorage.cs b/Wox.Infrastructure/Storage/BinaryStorage.cs index c0e8be63cf..e6bc9dd8c1 100644 --- a/Wox.Infrastructure/Storage/BinaryStorage.cs +++ b/Wox.Infrastructure/Storage/BinaryStorage.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using Wox.Infrastructure.Logger; @@ -23,6 +24,8 @@ namespace Wox.Infrastructure.Storage protected override void LoadInternal() { + //http://stackoverflow.com/questions/2120055/binaryformatter-deserialize-gives-serializationexception + AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; try { using (FileStream fileStream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) @@ -43,7 +46,32 @@ namespace Wox.Infrastructure.Storage catch (Exception) { serializedObject = LoadDefault(); +#if (DEBUG) + { + throw; + } +#endif } + finally + { + AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve; + } + } + + private System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + Assembly ayResult = null; + string sShortAssemblyName = args.Name.Split(',')[0]; + Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies(); + foreach (Assembly ayAssembly in ayAssemblies) + { + if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0]) + { + ayResult = ayAssembly; + break; + } + } + return ayResult; } protected override void SaveInternal() @@ -58,11 +86,11 @@ namespace Wox.Infrastructure.Storage catch (Exception e) { Log.Error(e.Message); - #if (DEBUG) - { - throw e; +#if (DEBUG) + { + throw; } - #endif +#endif } } } diff --git a/Wox/Themes/ThemeBuilder/Template.xaml b/Wox/Themes/ThemeBuilder/Template.xaml index c0978b4ba5..8d1c4845a4 100644 --- a/Wox/Themes/ThemeBuilder/Template.xaml +++ b/Wox/Themes/ThemeBuilder/Template.xaml @@ -2,7 +2,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> - +