序列化

2014-03-28

本文内容:序列化

概念:

 

序列化:将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,对象将其当前状态写入到临时或持久性存储区。以后,可以通过从存储区中读取 或反序列化对象的状态,重新创建对象。

需要注意的是对于任何可能包含重要的安全性数据的对象,应该使该对象不可序列化。如果它必须为可序列化的,请尝试生成特定字符来保存不可序列化的重要数据。

常见序列化方式:

二进制序列化、XML序列化、SOAP序列化、JSON序列化(BSON可以与其归为一类)

使用方法(c#)

二进制序列化:

使用前需要在类中加入Serializable特性

 

 [Serializable]
    public class Country
    {
        public string CountryName { get; set; }
        public string CityName { get; set; }
        public string AreaName { get; set; }
    }
View Code

 

           List<Country> list = new List<Country>();
            list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "滨江区" });
            list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "上城区" });
            list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "下沙区" });
            list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "西湖区" });
            list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "下城区" });

            Stream fs = new FileStream(@"a.dat", FileMode.Create,FileAccess.Write,FileShare.None);
            try
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, list);
            }
            catch (SerializationException se)
            {
                Console.WriteLine(se.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }
View Code
            Stream fs = new FileStream(@"a.dat", FileMode.Open,FileAccess.Read,FileShare.Read);
            try
            {
                BinaryFormatter bf = new BinaryFormatter();
                List<Country> clist = bf.Deserialize(fs) as List<Country>;
                clist.ForEach(
                    c => Console.WriteLine(c.CountryName + " " + c.CityName + " " + c.AreaName)
                    );
            }
            catch(SerializationException se)
            {
                Console.WriteLine(se.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }
View Code

XML序列化:

只对公共类的公共读/写属性,字段; 实现ICollection或IEnumerable的类;xmlElement对象;xmlNode对象;Dataset对象有效

需要在属性中加入XML特性

 

    [XmlRoot("XCountry")]
    public class XCountry
    {
        [XmlAttribute("CountryName")]
        public string CountryName { get; set; }
        [XmlElement("CityName")]
        public string CityName { get; set; }
        [XmlAttribute("AreaName")]
        public string AreaName { get; set; }
    }

   XCountry xc = new XCountry();
            xc.CountryName = "浙江省";
            xc.CityName = "杭州市";
            xc.AreaName = "滨江区";
            XmlSerializer xs = new XmlSerializer(typeof(XCountry));
            xs.Serialize(File.Create(@"b.dat"), xc);

XmlSerializer xs = new XmlSerializer(typeof(XCountry));
            XCountry xc = xs.Deserialize(File.OpenRead(@"b.dat")) as XCountry;
View Code

 

SOAP序列化跟二进制差不多

JSON序列化:

可以使用自带的JavaScriptSerializer 命名空间是System.Web.Script.Serialization

也可以使用第三方的Newtonsoft.Json(BSON也在里面)

 

  List<Country> list = new List<Country>();
            list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "滨江区" });
            list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "上城区" });
            list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "下沙区" });
            list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "西湖区" });
            list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "下城区" });
            string json = JsonConvert.SerializeObject(list);
            var lc = JsonConvert.DeserializeObject<List<Country>>(json);
            lc.ForEach(
                c => Console.WriteLine(c.CountryName + " " + c.CityName + " " + c.AreaName)
                );
View Code

 

 

  ref out小结:都作为引用传递 带ref参数方法使用前必须赋值,带out参数方法使用前将参数都清空,带out参数要在方法内部初始化赋值。

  ref作为输入,内部参数的修改都会改变该参数的值,而out一般是用作方法返回多个值。

 

 



posted on 2014-03-28 15:48  屌丝wei  阅读(217)  评论(0)    收藏  举报