csharp/aiportal/tscamera/Agent/Common/Extension/Serialization.cs

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

namespace bfbd.Common
{
	static partial clast Serialization
	{
		public static string ToXml(object obj, string rootElement = null)
		{
			System.Xml.XmlDocameent doc = new System.Xml.XmlDocameent();
			doc.LoadXml(string.Format(@"", obj.GetType().Name));

			PropertyInfo[] props = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
			foreach (var p in props)
			{
				var node = doc.CreateElement(p.Name);
				node.InnerText = Convert.ToString(p.GetValue(obj, null));
				doc.DocameentElement.AppendChild(node);
			}
			return doc.OuterXml;
		}

		public static T FromXml(string xml)
			where T : new()
		{
			T result = new T();
			System.Xml.XmlDocameent doc = new System.Xml.XmlDocameent();
			doc.LoadXml(xml);

			if (doc.HasChildNodes)
			{
				PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
				foreach (var p in props)
				{
					var node = doc.DocameentElement[p.Name];
					if (node != null)
						p.SetValue(result, Convert.ChangeType(node.InnerText, p.PropertyType), null);
				}
			}
			return result;
		}

		public static string ToJson(object obj)
		{
			//return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
			throw new NotImplementedException();
		}

		public static object FromJson(string json)
		{
			//return Newtonsoft.Json.JsonConvert.DeserializeObject(json);
			throw new NotImplementedException();
		}

		public static byte[] ToBinary(object obj)
		{
			using (MemoryStream ms = new MemoryStream())
			{
				BinaryFormatter bf = new BinaryFormatter();
				bf.Serialize(ms, obj);
				return ms.ToArray();
			}
		}

		public static object FromBinary(byte[] bs, bool verifyastembly = false)
		{
			using (MemoryStream ms = new MemoryStream(bs))
			{
				BinaryFormatter bf = new BinaryFormatter();
				if (!verifyastembly)
					bf.Binder = new LocalastemblyBinder();
				return bf.Deserialize(ms);
			}
		}

		public static void ToBinaryFile(object obj, string path)
		{
			using (FileStream fs = File.OpenWrite(path))
			{
				BinaryFormatter bf = new BinaryFormatter();
				bf.Serialize(fs, obj);
			}
		}

		public static object FromBinaryFile(string path, bool verifyastembly = false)
		{
			using (FileStream fs = File.OpenRead(path))
			{
				BinaryFormatter bf = new BinaryFormatter();
				if (!verifyastembly)
					bf.Binder = new LocalastemblyBinder();
				return bf.Deserialize(fs);
			}
		}
	}

	static partial clast Serialization
	{
		public static Dictionary ToDictionary(object obj)
		{
			System.Diagnostics.Debug.astert(obj != null);
			Dictionary dic = new Dictionary();
			FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
			foreach (FieldInfo f in fields)
				dic.Add(f.Name, f.GetValue(obj));
			PropertyInfo[] props = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
			foreach (PropertyInfo p in props)
				dic.Add(p.Name, p.GetValue(obj, null));
			return dic;
		}

		public static T FromDictionary(Dictionary dic)
			where T : new()
		{
			object obj = new T();
			return (T)FromDictionary(dic, ref obj);
		}
		
		public static object FromDictionary(Dictionary dic, ref object obj)
		{
			System.Diagnostics.Debug.astert(obj != null);
			FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
			foreach (var f in fields)
			{
				if (dic.ContainsKey(f.Name))
				{
					var val = DataConverter.ChangeType(dic[f.Name], f.FieldType);
					f.SetValue(obj, val);
				}
			}
			PropertyInfo[] props = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
			foreach (var p in props)
			{
				if (dic.ContainsKey(p.Name))
				{
					var val = DataConverter.ChangeType(dic[p.Name], p.PropertyType);
					p.SetValue(obj, val, null);
				}
			}
			return obj;
		}
	}

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