/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2017-08-17
 * 时间: 14:36
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace nativeCom
{
	public interface huMnemonic{
		void Clear();
	}
	
	public static class baseFuntion{
		
		/// <summary>
		/// 清空一个int数组
		/// </summary>
		/// <param name="array"></param>
		public static void ClearIntArray(ref int[] array){
			int len = array.Length;
			for(int i=0;i<len;i++){
				array[i] = 0;
			}
		}
		
		/// <summary>
		/// 把array1中的值完全拷贝到array2中
		/// </summary>
		/// <param name="array1">array1</param>
		/// <param name="array2">array2</param>
		/// <param name="idx">从那个开始拷贝</param>
		public static void CopyArray(ref int[] array1,ref int[] array2,int idx){
			int length = array1.Length;
			Array.Copy(array1,0,array2,idx,length);
		}
		
		/// <summary>
		/// 序列化
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="path"></param>
		public static void Serizeable(Object obj,string path){
//			try{
			FileStream stream = new FileStream(path,FileMode.OpenOrCreate);
			BinaryFormatter binary = new BinaryFormatter();
			binary.Serialize(stream,obj);
			stream.Close();
			stream.Dispose();
//			}catch(Exception e){
//				Form1.log("序列化失败:" + e.ToString());
//			}
		}
		
		/// <summary>
		/// 反序列化
		/// </summary>
		/// <param name="path"></param>
		/// <returns></returns>
		public static Object DeSerizeable(string path){
			if(!File.Exists(path))
				return null;
//			try{
			FileStream stream = new FileStream(path,FileMode.Open);
			BinaryFormatter binary = new BinaryFormatter();
			Object obj = binary.Deserialize(stream);
			stream.Close();
			stream.Dispose();
			return obj;
//			}catch(Exception e){
//				Form1.log("反序列化失败:" + e.ToString());
//				return null;
//			}
			
		}
		
		/// <summary>
		/// 序列化到内存流
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="path"></param>
		/// <returns></returns>
		public static MemoryStream Serizeable(Object obj){
			MemoryStream ms = null;
//			try{
			ms = new MemoryStream();
			BinaryFormatter binary = new BinaryFormatter();
			binary.Serialize(ms,obj);
//			}catch(Exception e){
//				Form1.log("序列化到内存失败:" + e.ToString());
//			}
			return ms;
		}
		
		/// <summary>
		/// 序列化内存流
		/// </summary>
		/// <param name="ms"></param>
		/// <returns></returns>
		public static Object DeSerizeable(MemoryStream ms){
			ms.Position = 0;
			Object obj;
//			try{
			BinaryFormatter binary = new BinaryFormatter();
			obj = binary.Deserialize(ms);
//			}catch(Exception e){
//				Form1.log("内存反序列化失败:" + e.ToString());
//				return null;
//			}
			return obj;
		}
		
		
		
		/// <summary>
		/// 克隆
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public static Object Clone(Object obj){
			
			MemoryStream ms = Serizeable(obj);
			
			Object tmp = DeSerizeable(ms);
			ms.Close();
			ms.Dispose();
			return tmp;
		}
		
		/// <summary>
		/// 字符数装数字
		/// </summary>
		/// <param name="ch">0-9</param>
		/// <returns></returns>
		public static sbyte charToInt(this char ch){
			return (sbyte)(ch-48);
		}
	}
}
