6 people like it.
Like the snippet!
Shrink your ISerializable boilerplate
A pair of extension methods for SerializationInfo that make custom ISerializable implementations much more straightforward to write.
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:
|
open System
open System.Runtime.Serialization
type SerializationInfo with
member s.Get<'T> ?param =
let param = typeof<'T>.FullName + "-" + defaultArg param ""
s.GetValue(param, typeof<'T>) :?> 'T
member s.Set<'T> (value : 'T, ?param) =
let param = typeof<'T>.FullName + "-" + defaultArg param ""
s.AddValue(param, value)
// example
[<Serializable>]
type Test(msg : string, inner : exn, foo : int, bar : int) =
inherit Exception(msg, inner)
new (s : SerializationInfo, _ : StreamingContext) =
Test(s.Get(), s.Get(), s.Get "foo", s.Get "bar")
member __.Foo = foo
member __.Bar = bar
interface ISerializable with
member __.GetObjectData(s : SerializationInfo, _ : StreamingContext) =
s.Set msg
s.Set inner
s.Set (foo, "foo")
s.Set (bar, "bar")
|
namespace System
namespace System.Runtime
namespace System.Runtime.Serialization
Multiple items
type SerializationInfo =
new : type:Type * converter:IFormatterConverter -> SerializationInfo
member AddValue : name:string * value:obj -> unit + 15 overloads
member AssemblyName : string with get, set
member FullTypeName : string with get, set
member GetBoolean : name:string -> bool
member GetByte : name:string -> byte
member GetChar : name:string -> char
member GetDateTime : name:string -> DateTime
member GetDecimal : name:string -> decimal
member GetDouble : name:string -> float
...
Full name: System.Runtime.Serialization.SerializationInfo
--------------------
SerializationInfo(type: Type, converter: IFormatterConverter) : unit
val s : SerializationInfo
member SerializationInfo.Get : ?param:string -> 'T
Full name: Script.Get
val param : string option
val param : string
val typeof<'T> : Type
Full name: Microsoft.FSharp.Core.Operators.typeof
val defaultArg : arg:'T option -> defaultValue:'T -> 'T
Full name: Microsoft.FSharp.Core.Operators.defaultArg
SerializationInfo.GetValue(name: string, type: Type) : obj
Multiple items
member SerializationInfo.Set : value:'T * ?param:string -> unit
Full name: Script.Set
--------------------
module Set
from Microsoft.FSharp.Collections
--------------------
type Set<'T (requires comparison)> =
interface IComparable
interface IEnumerable
interface IEnumerable<'T>
interface ICollection<'T>
new : elements:seq<'T> -> Set<'T>
member Add : value:'T -> Set<'T>
member Contains : value:'T -> bool
override Equals : obj -> bool
member IsProperSubsetOf : otherSet:Set<'T> -> bool
member IsProperSupersetOf : otherSet:Set<'T> -> bool
...
Full name: Microsoft.FSharp.Collections.Set<_>
--------------------
new : elements:seq<'T> -> Set<'T>
val value : 'T
SerializationInfo.AddValue(name: string, value: DateTime) : unit
(+0 other overloads)
SerializationInfo.AddValue(name: string, value: decimal) : unit
(+0 other overloads)
SerializationInfo.AddValue(name: string, value: float) : unit
(+0 other overloads)
SerializationInfo.AddValue(name: string, value: float32) : unit
(+0 other overloads)
SerializationInfo.AddValue(name: string, value: uint64) : unit
(+0 other overloads)
SerializationInfo.AddValue(name: string, value: int64) : unit
(+0 other overloads)
SerializationInfo.AddValue(name: string, value: uint32) : unit
(+0 other overloads)
SerializationInfo.AddValue(name: string, value: int) : unit
(+0 other overloads)
SerializationInfo.AddValue(name: string, value: uint16) : unit
(+0 other overloads)
SerializationInfo.AddValue(name: string, value: int16) : unit
(+0 other overloads)
Multiple items
type SerializableAttribute =
inherit Attribute
new : unit -> SerializableAttribute
Full name: System.SerializableAttribute
--------------------
SerializableAttribute() : unit
Multiple items
type Test =
inherit Exception
interface ISerializable
new : s:SerializationInfo * StreamingContext -> Test
new : msg:string * inner:exn * foo:int * bar:int -> Test
member Bar : int
member Foo : int
Full name: Script.Test
--------------------
new : s:SerializationInfo * StreamingContext -> Test
new : msg:string * inner:exn * foo:int * bar:int -> Test
val msg : string
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = String
Full name: Microsoft.FSharp.Core.string
val inner : exn
type exn = Exception
Full name: Microsoft.FSharp.Core.exn
val foo : int
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<_>
val bar : int
Multiple items
type Exception =
new : unit -> Exception + 2 overloads
member Data : IDictionary
member GetBaseException : unit -> Exception
member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
member GetType : unit -> Type
member HelpLink : string with get, set
member InnerException : Exception
member Message : string
member Source : string with get, set
member StackTrace : string
...
Full name: System.Exception
--------------------
Exception() : unit
Exception(message: string) : unit
Exception(message: string, innerException: exn) : unit
Exception(info: SerializationInfo, context: StreamingContext) : unit
Multiple items
type StreamingContext =
struct
new : state:StreamingContextStates -> StreamingContext + 1 overload
member Context : obj
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member State : StreamingContextStates
end
Full name: System.Runtime.Serialization.StreamingContext
--------------------
StreamingContext()
StreamingContext(state: StreamingContextStates) : unit
StreamingContext(state: StreamingContextStates, additional: obj) : unit
member SerializationInfo.Get : ?param:string -> 'T
member Test.Foo : int
Full name: Script.Test.Foo
val __ : Test
member Test.Bar : int
Full name: Script.Test.Bar
type ISerializable =
member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
Full name: System.Runtime.Serialization.ISerializable
override Test.GetObjectData : s:SerializationInfo * StreamingContext -> unit
Full name: Script.Test.GetObjectData
member SerializationInfo.Set : value:'T * ?param:string -> unit
More information