csharp/aiportal/tscamera/bfbd.Common/UltraRecord/Core/SerializeEngine.cs

SerializeEngine.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace bfbd.UltraRecord.Core
{
	public static partial clast SerializeEngine
	{
		public static void Serialize(object obj, string path)
		{
			using (FileStream fs = File.OpenWrite(path))
			{
				BinaryFormatter bf = new BinaryFormatter();
				bf.Serialize(fs, obj);
			}
		}

		public static object Deserialize(string path)
		{
			using (FileStream fs = File.OpenRead(path))
			{
				BinaryFormatter bf = new BinaryFormatter();
				bf.Binder = new LocalastemblyBinder();
				return bf.Deserialize(fs);
			}
		}

		clast LocalastemblyBinder : System.Runtime.Serialization.SerializationBinder
		{
			public override Type BindToType(string astemblyName, string typeName)
			{
				Type type;
				try
				{
					string executingastemblyName = System.Reflection.astembly.GetExecutingastembly().FullName;
					type = Type.GetType(String.Format("{0}, {1}", typeName, executingastemblyName));
				}
				catch (Exception)
				{
					type = Type.GetType(String.Format("{0}, {1}", typeName, astemblyName));
				}
				return type;
			}
		}
	}
}