SerializeHelper.cs

csharp

浏览数:1,012

2019-1-8


SerializeHelper.cs

public class SerializeHelper
    {
        /// 
        /// 将对象序列化成XML格式
        /// 
        /// 序列化得对象
        /// 对象类型
        /// 
        /// 
        public static string XmlSerialize(object entity, Type type)
        {
            XmlSerializer mySerializer = new XmlSerializer(type);
            MemoryStream myStream = new MemoryStream();
            XmlWriter writer = XmlWriter.Create(myStream);
            mySerializer.Serialize(writer, entity);
            writer.Flush();
            writer.Close();
            string xml = Encoding.UTF8.GetString(myStream.GetBuffer());
            myStream.Close();
            myStream.Dispose();
            return xml;
        }

        /// 
        /// 将XML反序列化成对象
        /// 
        /// XML
        /// 
        /// 
        /// 
        public static T XmlDeserialize(string xml)
        {
            XmlSerializer mySerializer = new XmlSerializer(typeof(T));

            using (MemoryStream myFileStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                T myObject = (T)mySerializer.Deserialize(myFileStream);
                return myObject;
            }
        }

        /// 
        /// 将对象序列化成二进制流
        /// 
        /// 
        /// 
        public static byte[] Serialize(object entity)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, entity);
                return stream.ToArray();
            }
        }

        /// 
        /// 将二进制流反序列化成对象
        /// 
        /// 
        /// 
        public static T Deserialize(byte[] serialize)
        {
            BinaryFormatter ser = new BinaryFormatter();
            using (MemoryStream mem = new MemoryStream(serialize))
            {
                return (T)ser.UnsafeDeserialize(mem, null);
            }
        }

        /// 
        /// 拆分字节
        /// 
        /// 
        /// 
        /// 
        public static List SplitBytes(byte[] splitSource, int splitLength)
        {
            List result = new List();
            int splitSourceLength = splitSource.Length;
            int splitCount = splitSourceLength / splitLength + ((splitSourceLength % splitLength) > 0 ? 1 : 0);
            for (int i = 0; i < splitCount; i++)
            {
                int copyStartIndex = i * splitLength;
                int copyLength = splitLength;
                copyLength = (copyStartIndex + copyLength) > splitSourceLength ? splitSourceLength - copyStartIndex : copyLength;
                byte[] content = new byte[copyLength];
                Array.Copy(splitSource, copyStartIndex, content, 0, copyLength);
                result.Add(content);
            }
            return result;
        }

        /// 
        /// 合并字节
        /// 
        /// 
        /// 
        public static byte[] MergeBytes(List source)
        {
            int sourceLength = 0;
            foreach (var item in source)
            {
                sourceLength += item.Length;
            }
            byte[] result = new byte[sourceLength];
            int currentIndex = 0;
            foreach (var item in source)
            {
                Array.Copy(item, 0, result, currentIndex, item.Length);
                currentIndex += item.Length;
            }
            return result;
        }

        /// 
        /// 将对象序列化成Json格式
        /// 
        /// 序列化对象
        /// 
        public static string JsonSerialize(object entity)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(entity);
        }

        /// 
        /// 将Json反序列化成对象
        /// 
        /// 对象类型
        /// json字符串
        /// 
        public static T JsonDeserialize(string json)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Deserialize(json);
        }
    }