Exemple #1
0
        public string Serialize(T t)
        {
            string          result    = "";
            SerializeObject objcopy   = null;
            var             xmlformat = new XmlSerializer(typeof(SerializeObject));
            var             binformat = new BinaryFormatter();

            //xml-stream,序列化流
            using (var st = new MemoryStream())
            {
                xmlformat.Serialize(st, t);
                st.Position = 0;
                objcopy     = xmlformat.Deserialize(st) as SerializeObject;

                st.Position = 0;
                byte[] temp = new byte[1024 * 1024 * 4];
                st.Read(temp, 0, temp.Length);
                result = Encoding.Default.GetString(temp);
            }


            //xml-textwriter-textreader,序列化字符串
            using (var sw = new StringWriter())
            {
                xmlformat.Serialize(sw, t);
                result = sw.ToString();


                using (var sr = new StringReader(result))
                {
                    objcopy = xmlformat.Deserialize(sr) as SerializeObject;
                }
            }

            //json+simplejson
            result  = SimpleJsonHelper.JsonSerializer(t);
            objcopy = SimpleJsonHelper.JsonDeserialize <SerializeObject>(result);
            return(result);
        }