标签:fsharp serializer
设计之初就需要考虑的要点有open System open 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() result let 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 d let ms = new StreamReader("serialization.xml") let newd = ser.Deserialize(ms) :?> DateSerTest if d = newd then printfn "Equal"
标签:fsharp serializer
原文地址:http://blog.csdn.net/samwell/article/details/41548221