6 people like it.

Serialization of functions

Serialization and deserialization of a function value with Soap formatting. (With serious limitations; not sure how useful this is.)

 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: 
// Serialization of function values
// (Deserialization will only work from within
//  a completely identical binary.)
open System.IO 
open System.Runtime.Serialization.Formatters.Soap

let i = ref 0 
let s = ref "hello world" 

let op () = 
  i := !i + 1 
  s := !s + "!" 

let sf = new SoapFormatter();

let choice = int(System.Console.ReadLine())
if choice = 0 then
 use fs = new FileStream("op.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite)
 sf.Serialize(fs, op);
else
 use fs2 = new FileStream("op.xml", FileMode.Open, FileAccess.Read)
 let op2 = sf.Deserialize(fs2) :?> (unit->unit)
 op2()
 op2()
 printfn "%d %s" !i !s
namespace System
namespace System.IO
namespace System.Runtime
namespace System.Runtime.Serialization
namespace System.Runtime.Serialization.Formatters
namespace System.Runtime.Serialization.Formatters.Soap
val i : int ref

Full name: Script.i
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val s : string ref

Full name: Script.s
val op : unit -> unit

Full name: Script.op
val sf : SoapFormatter

Full name: Script.sf
Multiple items
type SoapFormatter =
  new : unit -> SoapFormatter + 1 overload
  member AssemblyFormat : FormatterAssemblyStyle with get, set
  member Binder : SerializationBinder with get, set
  member Context : StreamingContext with get, set
  member Deserialize : serializationStream:Stream -> obj + 1 overload
  member FilterLevel : TypeFilterLevel with get, set
  member Serialize : serializationStream:Stream * graph:obj -> unit + 1 overload
  member SurrogateSelector : ISurrogateSelector with get, set
  member TopObject : ISoapMessage with get, set
  member TypeFormat : FormatterTypeStyle with get, set

Full name: System.Runtime.Serialization.Formatters.Soap.SoapFormatter

--------------------
SoapFormatter() : unit
SoapFormatter(selector: System.Runtime.Serialization.ISurrogateSelector, context: System.Runtime.Serialization.StreamingContext) : unit
val choice : int

Full name: Script.choice
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
System.Console.ReadLine() : string
val fs : FileStream
Multiple items
type FileStream =
  inherit Stream
  new : path:string * mode:FileMode -> FileStream + 14 overloads
  member BeginRead : array:byte[] * offset:int * numBytes:int * userCallback:AsyncCallback * stateObject:obj -> IAsyncResult
  member BeginWrite : array:byte[] * offset:int * numBytes:int * userCallback:AsyncCallback * stateObject:obj -> IAsyncResult
  member CanRead : bool
  member CanSeek : bool
  member CanWrite : bool
  member EndRead : asyncResult:IAsyncResult -> int
  member EndWrite : asyncResult:IAsyncResult -> unit
  member Flush : unit -> unit + 1 overload
  member GetAccessControl : unit -> FileSecurity
  ...

Full name: System.IO.FileStream

--------------------
FileStream(path: string, mode: FileMode) : unit
   (+0 other overloads)
FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: FileAccess) : unit
   (+0 other overloads)
FileStream(path: string, mode: FileMode, access: FileAccess) : unit
   (+0 other overloads)
FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: FileAccess, bufferSize: int) : unit
   (+0 other overloads)
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare) : unit
   (+0 other overloads)
FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: FileAccess, bufferSize: int, isAsync: bool) : unit
   (+0 other overloads)
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare, bufferSize: int) : unit
   (+0 other overloads)
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare, bufferSize: int, options: FileOptions) : unit
   (+0 other overloads)
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare, bufferSize: int, useAsync: bool) : unit
   (+0 other overloads)
FileStream(path: string, mode: FileMode, rights: System.Security.AccessControl.FileSystemRights, share: FileShare, bufferSize: int, options: FileOptions) : unit
   (+0 other overloads)
type FileMode =
  | CreateNew = 1
  | Create = 2
  | Open = 3
  | OpenOrCreate = 4
  | Truncate = 5
  | Append = 6

Full name: System.IO.FileMode
field FileMode.OpenOrCreate = 4
type FileAccess =
  | Read = 1
  | Write = 2
  | ReadWrite = 3

Full name: System.IO.FileAccess
field FileAccess.ReadWrite = 3
SoapFormatter.Serialize(serializationStream: Stream, graph: obj) : unit
SoapFormatter.Serialize(serializationStream: Stream, graph: obj, headers: System.Runtime.Remoting.Messaging.Header []) : unit
val fs2 : FileStream
field FileMode.Open = 3
field FileAccess.Read = 1
val op2 : (unit -> unit)
SoapFormatter.Deserialize(serializationStream: Stream) : obj
SoapFormatter.Deserialize(serializationStream: Stream, handler: System.Runtime.Remoting.Messaging.HeaderHandler) : obj
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view Test code New version

More information

Link:http://fssnip.net/2R
Posted:13 years ago
Author:wmeyer
Tags: serialization