If you have an object whose run-time type is more specific than its compile-time type, then you can recover the type information with a downcast or a type test.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 

let oa : obj[] = [| box 42; box "hello" |]
let o : obj = box oa

// now recover types
// I know o is actually an object array at runtime
let newoa = o :?> obj[]
// I know the first element is an int
let i = newoa.[0] :?> int
printfn "%d" i
// if I'm not sure, I can type test
match newoa.[1] with
| :? int as i -> printfn "the int %d" i
| :? string as s -> printfn "the string %s" s
| _ -> printfn "something else"

By on 1/27/2010 11:29 AM ()

Aha - This works! Thanks so much.

By on 1/27/2010 1:27 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper