C#中如何通過類的靜態屬性完成XML序列化?

來源:互聯網
上載者:User

預設情況下,靜態類無法被序列化,因為無法執行個體化對象;類的靜態屬性無法序列化,因為靜態屬性屬於類而非具體執行個體。那麼怎樣才能執行個體化靜態屬性呢?可以用下面的方法:

建議看英文版:http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/687d1222-404e-4009-974e-9189280faa96

首先,XML序列化可以通過BinaryFormatter或者SoapFormatter實現。請按照以下步驟通過類的靜態屬性完成XML序列化。
1. 讓類的靜態屬性實現ISerializable並且把這個類設為Serializable。
2. 實現GetObjectData方法。
3. 建立一個和GetObjectData方法相同結構的構造方法。

序列化過程中,這個類實際上調用了GetObjectData方法。還原序列化時,類調用相同的構造方法執行個體化。
更多細節的內容,請參閱http://msdn.microsoft.com/en-us/magazine/cc301767.aspx

這裡是含有靜態屬性的類:
    [Serializable]
    public class MyClass : ISerializable
    {
        static int? sindex;
        static int? len;

        public static int? StartIndex
        {
            get { return sindex; }
            set { sindex = value; }
        }
        public static int? Length
        {
            get { return len; }
            set { len = value; }
        }

        public MyClass()
        { }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("static.sindex", MyClass.sindex, typeof(int));
            info.AddValue("static.len", MyClass.len, typeof(int));
        }

        public MyClass(SerializationInfo info, StreamingContext context)
        {
            MyClass.sindex = info.GetInt32("static.sindex");
            MyClass.len = info.GetInt32("static.len");
        }
    }

一段序列化和還原序列化的代碼:
        private void BinarySerialize()
        {
            FileStream fileStream = new FileStream("test.binary", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fileStream, myClass);
            fileStream.Dispose();
        }
        private void BinaryDeserialize()
        {
            FileStream fileStream = new FileStream("test.binary", FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();
            MyClass myClass = (MyClass)bf.Deserialize(fileStream);
            MessageBox.Show(MyClass.StartIndex.HasValue.ToString());
            MessageBox.Show(MyClass.StartIndex.Value.ToString());
        }
        private void SoapSerialize()
        {
            FileStream fileStream = new FileStream("test.soap", FileMode.Create);
            SoapFormatter sf = new SoapFormatter();
            sf.Serialize(fileStream, myClass);
            fileStream.Dispose();
        }
        private void SoapDeserialize()
        {
            FileStream fileStream = new FileStream("test.soap", FileMode.Open);
            SoapFormatter sf = new SoapFormatter();
            MyClass myClass = (MyClass)sf.Deserialize(fileStream);
            MessageBox.Show(MyClass.StartIndex.HasValue.ToString());
            MessageBox.Show(MyClass.StartIndex.Value.ToString());
        }
相關的文章:
http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/687d1222-404e-4009-974e-9189280faa96

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.