using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.Serialization; namespace PaintDotNet.SystemLayer { /// /// This is an implementation of SerializationBinder that tries to find a match /// for a type even if a direct match doesn't exist. This gets around versioning /// mismatches, and allows you to move data types between assemblies. /// /// /// This class is in SystemLayer because there is code in this assembly that must /// make use of it. This class does not otherwise need to be here, and can be /// ignored by implementors. /// public sealed class SerializationFallbackBinder : SerializationBinder { private List assemblies; public SerializationFallbackBinder() { this.assemblies = new List(); } public void AddAssembly(Assembly assembly) { this.assemblies.Add(assembly); } private Type TryBindToType(Assembly assembly, string typeName) { Type type = assembly.GetType(typeName, false, true); return type; } public override Type BindToType(string assemblyName, string typeName) { Type type = null; foreach (Assembly tryAssembly in this.assemblies) { type = TryBindToType(tryAssembly, typeName); if (type != null) { break; } } if (type == null) { string fullTypeName = typeName + ", " + assemblyName; try { type = System.Type.GetType(fullTypeName, false, true); } catch (FileLoadException) { type = null; } } return type; } } }