持久化(Serialize)探索嘗試,serialize探索嘗試

來源:互聯網
上載者:User

持久化(Serialize)探索嘗試,serialize探索嘗試
設計之初就需要考慮的要點有
1.資料結構是否支援Web Service
2.是否需要支援持久化對象為XML結構,與Contract持久化並不衝突,是一種補充
3.當.net對象垮遠程邊界時,考慮支援runtime持久化
4.當只需要普通持久化時,避免使用XML和Runtime技術。

XML Serial 
用了Dotnet那麼久還幾乎沒用過Serial技術,慚愧。

open Systemopen System.IO[<CLIMutable>]//這裡必須添加此特性,讓這個Recode帶有一個預設的建構函式type DateSerTest =     {        Local:DateTime;        Utc:DateTime    }let ser = new System.Xml.Serialization.XmlSerializer(typeof<DateSerTest>)let d = {    Local = DateTime.SpecifyKind(new DateTime(2014, 10, 1), DateTimeKind.Local)    Utc = DateTime.SpecifyKind(new DateTime(2014, 10, 1), DateTimeKind.Utc)}let testSerialization(dt:DateSerTest) =    use ms = new IO.MemoryStream()    ser.Serialize(ms, o = dt)    let result = Text.Encoding.Default.GetString(ms.GetBuffer())    ms.Close()    resultlet objecblock = testSerialization d//從記憶體流中取得Xml位元組流,流是資料是媒介,Serializer是模板。let ms = new IO.MemoryStream(Text.Encoding.Unicode.GetBytes(objecblock))let newd = ser.Deserialize(ms) :?> DateSerTest//產生對象if d = newd then prrintfn "Equal"

將記憶體流修改為檔案流,再進行下面的測試,可以得到相同的結果。

let testFileSerialization(dt:DateSerTest) =    let filename = "serialization.xml"    use ms = new StreamWriter(filename)    ser.Serialize(ms, o = dt)    ms.Close()testFileSerialization dlet ms = new StreamReader("serialization.xml")let newd = ser.Deserialize(ms) :?> DateSerTestif d = newd then printfn "Equal"

類可以更複雜,特殊的情況需要添加特定的Attribute進行特殊的定義。以上

聯繫我們

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