How to cast an object to a list of generic type in F?

Unfortunately, there's no easy way to do what you want. Type tests can only be used with specific types, and even if the type test passed, the conversion operator :? Also only works to cast expressions to specific types so the right hand side of your match wouldn't do what you want anyway.

You can partially work around this issue using an active pattern: open Microsoft.FSharp. Quotations open Microsoft.FSharp.Quotations. Patterns let ( |GenericType|_| ) = (* methodinfo for typedefof *) let tdo = let (Call(None,t,)) = t.

GetGenericMethodDefinition() (* match type t against generic def g *) let rec tymatch t (g:Type) = if t = typeof then None elif g. IsInterface then let ints = if t. IsInterface then |t| else t.GetInterfaces() ints |> Seq.

TryPick (fun t -> if (t. GetGenericTypeDefinition() = g) then Some(t. GetGenericArguments()) else None) elif t.

IsGenericType && t. GetGenericTypeDefinition() = g then Some(t. GetGenericArguments()) else tymatch (t.

BaseType) g fun (e:Expr) (t:Type) -> match e with | Call(None,mi,) -> if (mi. GetGenericMethodDefinition() = tdo) then let |ty| = mi. GetGenericArguments() if ty.

IsGenericType then let tydef = ty. GetGenericTypeDefinition() tymatch t tydef else None else None | _ -> None This active pattern can be used as follows: match o.GetType() with | GenericType @> |t| -> addChildListUntyped(t,o) | _ -> addChild(o) where you've created a variation of addChildList which takes a type t and an object o (with runtime type list.

Unfortunately, there's no easy way to do what you want. Type tests can only be used with specific types, and even if the type test passed, the conversion operator :? > also only works to cast expressions to specific types so the right hand side of your match wouldn't do what you want anyway.

You can partially work around this issue using an active pattern: open Microsoft.FSharp. Quotations open Microsoft.FSharp.Quotations. Patterns let ( |GenericType|_| ) = (* methodinfo for typedefof *) let tdo = let (Call(None,t,)) = t.

GetGenericMethodDefinition() (* match type t against generic def g *) let rec tymatch t (g:Type) = if t = typeof then None elif g. IsInterface then let ints = if t. IsInterface then |t| else t.GetInterfaces() ints |> Seq.

TryPick (fun t -> if (t. GetGenericTypeDefinition() = g) then Some(t. GetGenericArguments()) else None) elif t.

IsGenericType && t. GetGenericTypeDefinition() = g then Some(t. GetGenericArguments()) else tymatch (t.

BaseType) g fun (e:Expr) (t:Type) -> match e with | Call(None,mi,) -> if (mi. GetGenericMethodDefinition() = tdo) then let |ty| = mi. GetGenericArguments() if ty.

IsGenericType then let tydef = ty. GetGenericTypeDefinition() tymatch t tydef else None else None | _ -> None This active pattern can be used as follows: match o.GetType() with | GenericType @> |t| -> addChildListUntyped(t,o) | _ -> addChild(o) where you've created a variation of addChildList which takes a type t and an object o (with runtime type list) instead of taking a generic list. This is a bit clunky, but I can't think of a cleaner solution.

Seq) | _ -> addChild(o) I don't really care that it is a list.As long as I can iterate over it.

That ought to work on . NET 4.0, but will not work on previous versions as seq is not marked as covariant. Also, keep in mind that this will only work on lists or arrays containing reference types (e.g. A list can be treated as a seq, but a list can't).

– kvb Mar 30 '10 at 22:14 Also, I think it would be slightly cleaner to do the pattern match like | :? Seq as s -> addChildCollection(s) so you don't have an explicit downcast. – kvb Mar 30 '10 at 22:16.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions