English 中文(简体)
在什么地方,我可以找到F#受歧视的工会的序号?
原标题:Where can I find a serializer for F# discriminated unions?

我需要坚持一种抽象的yn子树,用F#受歧视的结合作为代人类阅读的契约形式,例如F#语言中已经使用的构建受歧视的结合的形式,我可以稍后再读到受歧视的工会。 我感到惊讶的是,F#图书馆没有为此提供支持,因为它肯定必须以高效的方式在汇编者和F#互动中做到。

是否以合理(但不一定极端)高效率的方式执行免费/开放源?

注:我不想以XML为基础的序列化。

问题回答

EDIT:以下答复都没有真正达到你的标准,但是,如果其他寻求加入工会的人认为这些答复是有用的。 我不了解任何图书馆如何重新编造工会的“%A”

作为小型抽样方案的一部分,这是一个工会序列化战略。 (KnownTypeAttribute>,可采用一种方法名称,并可使用某种思考来获取这些类型。) 这非常容易地在工会中增加一极小的法典范围,以达到序列化。

open Microsoft.FSharp.Reflection 
open System.Reflection 
open System.Runtime.Serialization 
open System.Xml

[<KnownType("KnownTypes")>]
type Union21WithKnownTypes = 
    | Case1 of int * int
    | Case2 of string
    static member KnownTypes() = 
        typeof<Union21WithKnownTypes>.GetNestedTypes(
            BindingFlags.Public 
            ||| BindingFlags.NonPublic) |> Array.filter FSharpType.IsUnion 

let dcs = new DataContractSerializer(typeof<Union21WithKnownTypes[]>)
let arr = [| Case1(1,1); Case2("2") |]
printfn "orig data: %A" arr
let sb = new System.Text.StringBuilder()
let xw = XmlWriter.Create(sb)
dcs.WriteObject(xw, arr)
xw.Close()
let s = sb.ToString()
printfn ""
printfn "encoded as: %s" s
printfn ""
let xr = XmlReader.Create(new System.IO.StringReader(s))
let o = dcs.ReadObject(xr)
printfn "final data: %A" o

* E/CN.6/2009/1。

open Microsoft.FSharp.Reflection  
open System.Reflection  
open System.Runtime.Serialization  
open System.Runtime.Serialization.Json  
open System.Xml 

[<KnownType("KnownTypes")>] 
type Union21WithKnownTypes =  
    | Case1 of int * int 
    | Case2 of string 
    static member KnownTypes() =  
        typeof<Union21WithKnownTypes>.GetNestedTypes( 
            BindingFlags.Public  
            ||| BindingFlags.NonPublic) |> Array.filter FSharpType.IsUnion  

let dcs = new DataContractJsonSerializer(typeof<Union21WithKnownTypes[]>) 
let arr = [| Case1(1,1); Case2("2") |] 
printfn "orig data: %A" arr 
let stream = new System.IO.MemoryStream()
dcs.WriteObject(stream, arr) 
stream.Seek(0L, System.IO.SeekOrigin.Begin) |> ignore
let bytes = Array.create (int stream.Length) 0uy
stream.Read(bytes, 0, int stream.Length) |> ignore
let s = System.Text.Encoding.ASCII.GetString(bytes)
printfn "" 
printfn "encoded as: %s" s 
printfn "" 
stream.Seek(0L, System.IO.SeekOrigin.Begin) |> ignore
let o = dcs.ReadObject(stream)
printfn "final data: %A" o 




相关问题
Choosing the right subclass to instantiate programmatically

Ok, the context is some serialization / deserialization code that will parse a byte stream into an object representation that s easier to work with (and vice-versa). Here s a simplified example ...

WCF Problem Sending Object To Client

Background Converting from using .Net Remoting to WCF. Most of the methods on the WCF server are working fine, but ran into one that isn t working today. This is the service contract: [...

WCF DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

deserialize the XML Document --- Need Help

I am using the below code snippet now to deserialize the XML document ... [WebMethod] public XmlDocument OrderDocument(XmlDocument xmlDoc) { XmlSerializer serializer = new XmlSerializer(typeof(...

How do I serialize a child class?

how do I include the serialized data from a child class where both impliment iserializeable? Class A Implements ISerializable dim _B as new B Class B Implements ISerializable ...

WCF: Serialize complex objects with read-only members

Looking for some guidance on a WCF service I’m prototyping. I have a WCF service hosted in IIS that will pass data to my clients. I have a separate shared assembly that contains all my business ...

Long/multiple SQL queries vs Serialization

I m trying to improve the performance of my web app where a page does a long query to pull data from different tables on a database. It pulls invoice data with multiple item lines, invoice status, and ...

热门标签