Hi,

I believe you can use DataContractSerializer with some success. Take a look at this thread for more details.

regards,

Danny

By on 4/28/2009 2:34 AM ()

Hi, as i know the DataContractSerializer is from .NET 3.5, but the F# use 2.0

By on 4/28/2009 2:03 PM ()

I think the DataContractSerializer is actually .Net 3.0, but either way, if you are deploying to a box that only has .Net 2.0 then that will not work for you.

Below is a short program that works for me with BinaryFormatter; perhaps you can share more detail of code that is failing and error you see?

(Caveat: I did not try code below on CTP version of F#, but I don't think anything changed here.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 

#light

open System.IO
open System.Runtime.Serialization.Formatters.Binary 

let writeValue (outputStream:Stream) (x:'a) =
    let formatter = new BinaryFormatter()
    formatter.Serialize(outputStream,box x)

let readValue (inputStream:Stream) =
    let formatter = new BinaryFormatter()
    let res = formatter.Deserialize(inputStream)
    unbox res

let s = new MemoryStream()

type MyRec = { f1 : string; f2: int }

let r = {f1="hi"; f2=42}

writeValue s r

s.Seek(0L, SeekOrigin.Begin) |> ignore  // reset stream

let z : MyRec = readValue s

printfn "%A" z

By on 4/28/2009 4:51 PM ()

Yes, this is works in CTP too.

But in my code the type definition of MyRec is in my F# library(mylib.dll), and when i try to deserialize, i got exception: "unable to find assembly: mylib.dll"
The project that serialize, the mylib.dll, and the serialized file is in the same directory.

By on 4/29/2009 4:00 AM ()

I guess you will have to give us some more infos. For example the serialize/deserialize code.

Did you have a look at the MSDN documentation to BinaryFormatter, IFormatter and, ISerializable?

If you want to serialize your own objects you normally add the [Serializable] attribute to the class definition or implement the ISerializable interface - the formatters to the rest of the work but of course the code that deserializes your objects need to know the object-definition (that is in your case the class-definition) of the deserialized data.

By on 4/27/2009 9:34 PM ()

Hi, thanks for reply

This is my read and write function:

let writeValue (outputStream:Stream) (x:'a) =
//XML
//let serializer = new XmlSerializer(typeof<defaultLayout * Layout>)
//serializer.Serialize(outputStream,box x)

//Binary
let formatter = new BinaryFormatter()
formatter.Serialize(outputStream,box x)

let readValue (inputStream:Stream) =

//XML
//let serializer = new XmlSerializer(typeof<defaultLayout * Layout>)
//let res = serializer.Deserialize(inputStream)

//Binary
let formatter = new BinaryFormatter()
let res = formatter.Deserialize(inputStream)
unbox res

With BinaryFormatter the serialization works fine, but when i try deserialize, the program could not find my dll file(the serialzed record's type definitions is in it), which is in the same directory. Is there any workaround for it?

The XmlSerializer is not good for records, any other idea?

By on 4/28/2009 2:02 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper