I donβt think itβs so simple (remember that Iβm naive), consider the following scenario in which
1) we use generics on several types 2) we do not have type information for the object, so it is part of the obj type function, as in some datacontract / serialization.NET libraries.
I reworked my suggestion to use reflection:
type SomeType<'A> = { item : 'A } type AnotherType<'A> = { someList : 'A list } let test() = let getIt() : obj = let results : SomeType<AnotherType<int>> = { item = { someList = [1;2;3] }} upcast results let doSomething (results : obj) = let resultsType = results.GetType() if resultsType.GetGenericTypeDefinition() = typedefof<SomeType<_>> then let method = resultsType.GetMethod("get_item") if method <> null then let arr = method.Invoke(results, [||]) if arr.GetType().GetGenericTypeDefinition() = typedefof<AnotherType<_>> then printfn "match" getIt() |> doSomething
There seems to be a more natural way to do this ...
akaphenom
source share