今天的主角就是protobuf-net

來源:互聯網
上載者:User

標籤:

前言,我要解決的問題是,.NET的序列化問題,以前序列化Model,一旦當這個Model的欄位發生了變化,那麼就慘了。無法讀取。必須重新來過。今天搞定它啦。

 

 

點擊build-data進行建立data資料。

private void button1_Click(object sender, EventArgs e) {            List<User> list = new List<User>();            for (int i = 0; i < 1000; i++) {                //list.Add(new User() { UserID = i, UserName = "u" + i.ToString(), UserName2 = "u2" + i.ToString() });                list.Add(new User() { UserID = i, UserName = "u" + i.ToString() });            }            //protobuff             string path = AppDomain.CurrentDomain.BaseDirectory + "protobuf.txt";            using (Stream file = File.Create(path)) {                Serializer.Serialize<List<User>>(file, list);                file.Close();            }        }

看看txt檔案中的資料樣子。

 

點擊show-data顯示剛剛建立的資料。

無法增減User類中的欄位。

[ProtoContract]    public class User {        [ProtoMember(1, IsRequired = true)]        public int UserID { get; set; }        [ProtoMember(2, IsRequired = true)]        public string UserName { get; set; }        //[ProtoMember(3)]        //public string UserName2 { get; set; }    }

均可以正常讀取。歐葉。

 

源碼下載:http://files.cnblogs.com/renzhe/protobufTest.zip

今天的主角就是protobuf-net

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.