首頁  >  文章  >  後端開發  >  C#中序列化的使用總結

C#中序列化的使用總結

黄舟
黄舟原創
2017-08-08 14:14:171300瀏覽

貼一下自己序列化的程式碼:

public class XMLUtil
    {
        /// <summary>
        /// XML & Datacontract Serialize & Deserialize Helper
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serialObject"></param>
        /// <returns></returns>
        public static string Serializer<T>(T serialObject) where T : class
        {
            string result = string.Empty;
            using (MemoryStream mem = new MemoryStream())
            {
                using (XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8))
                {
                    System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    ser.Serialize(writer, serialObject);
                    result = Encoding.UTF8.GetString(mem.ToArray());
                }
            }
            return result;
        }

        public static T Deserialize<T>(string str) where T : class
        {
            T result = null;
            using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(str)))
            {
                using (StreamReader streamReader = new StreamReader(memoryStream))
                {
                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    result = (T)xmlSerializer.Deserialize(memoryStream);
                }
            }
            return result;
        }
    }

上面的寫法持續序列化不會有記憶體溢出的效能問題,之前一直被告知直接引用公司某位老鳥封裝好的dll來序列化,後來發現了老是出現記憶體溢出,貼一下它的錯誤寫法,僅供吸取教訓:

public class XMLUtil
    {
        /// <summary>
        /// XML & Datacontract Serialize & Deserialize Helper
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serialObject"></param>
        /// <returns></returns>
        public static string Serializer<T>(T serialObject) where T : class
        {
            //try
            //{
            XmlSerializer ser = new XmlSerializer(typeof(T));
            System.IO.MemoryStream mem = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8);
            ser.Serialize(writer, serialObject);
            writer.Close();

            return Encoding.UTF8.GetString(mem.ToArray());
            //}
            //catch (Exception ex)
            //{
            //    return null;
            //}
        }

        public static T Deserialize<T>(string str) where T : class
        {
            //try
            //{
            XmlSerializer mySerializer = new XmlSerializer(typeof(T));
            StreamReader mem2 = new StreamReader(
                    new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str)),
                    System.Text.Encoding.UTF8);

            return (T)mySerializer.Deserialize(mem2);
            //}
            //catch (Exception)
            //{
            //    return null;
            //}
        }

        //public static string Json_SerializeObject(object value)
        //{
        //    return Newtonsoft.Json.JsonConvert.SerializeObject(value);
        //}
        //public static object Json_DeserializeObject(string value)
        //{
        //    return Newtonsoft.Json.JsonConvert.DeserializeObject(value);
        //}
        //public static T Json_DeserializeObject<T>(string value)
        //{
        //    return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(value);
        //}
    }

以上是C#中序列化的使用總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn