jeudi 6 mai 2021

System.InvalidCastException: Specified cast is not valid when trying to use Reflection in F#

I am trying to construct a record type in F#. But the following code is giving System.InvalidCastException. I am new to F# and having a difficult time understanding as to why this isn't working. A detailed explanation would be much appreciated.

open System
open Microsoft.FSharp.Reflection

type Employee = {
    Id:          Guid
    Name:        string
    Phone:       string
    BadgeId:     string
    Designation: string
}

let values =
    [
        "bc07e94c-b376-45a2-928b-508b888802c9"
        "A"
        "B"
        "C"
        "D"
    ]
    |> Seq.ofList

let creator = FSharpValue.PreComputeRecordConstructor(typeof<Employee>, System.Reflection.BindingFlags.Public)

let x =
    (
        creator [|
            values
            |> Seq.map (fun y -> y)
        |]

    ) :?> Employee

printfn "%A" x




Aucun commentaire:

Enregistrer un commentaire