I am trying to process the contents of a dictionary based on the type of contained element (String, Int, Datetime etc). The following is a small test snippet which loads some test data and then extracts by type.
However I get the following constraint mismatch error which I don't understand - any ideas?
Error Info:
unmapdict.fsx(29,23): error FS0193: Type constraint mismatch.
The type 'String' is not compatible with the type 'Type'
open System
open System.Collections.Generic
// Type for dict entries
type dStruct = {
ftype: Type;
fvalue:obj;
}
let main() =
printfn "\n>>>>>> Extract Dictionary Elements <<<<< \n"
let ddict = new Dictionary<string, dStruct>()
let str = "String Data"
let rstring = { ftype=str.GetType(); fvalue=str ;}
ddict.Add("String", rstring)
let intn = 999
let rint32 = { ftype=intn.GetType(); fvalue=intn ;}
ddict.Add("Int32", rint32)
let rdatetime = {ftype=System.DateTime.Now.GetType(); fvalue=System.DateTime.Now}
ddict.Add("DateTime", rdatetime)
// Extract dict value elements; emumerate data types
ddict
|> Seq.map ( fun (KeyValue(k,v)) -> v)
|> Seq.iter (fun v ->
// Error occurs here >>
match v.ftype with
| :?System.String -> printfn "String Found ";
| :?System.Int32 -> printfn "Integer Found ";
| :?System.DateTime -> printfn "DateTime Found ";
|_ -> printfn "Unmatched Element"
// printfn "Dict: ftype: %A; fvalue: %A" v.ftype v.fvalue
)
printfn "\n>>>>>> F# Done <<<<< \n"
main()