F# equivalent of the C# typeof(IEnumerable<>)

Posted by Rune FS on Stack Overflow See other posts from Stack Overflow or by Rune FS
Published on 2010-03-19T20:04:01Z Indexed on 2010/03/19 20:21 UTC
Read the original article Hit count: 157

Filed under:
|

I have a piece of code where I need to figure out if a given type implements IEnumerable (I don't care about the T)

I've tried (t:System.Type in case you wonder)

let interfaces = t.GetInterfaces()
let enumerbale = interfaces.Any(
                     fun t -> (t.GetGenericTypeDefinition() = typeof<IEnumerable<>>) 

however that wont compile (the compile don't like the <>). I then tried

let interfaces = t.GetInterfaces()
let enumerbale = interfaces.Any(
                     fun t -> (t.GetGenericTypeDefinition() = typeof<IEnumerable<'a>>)

but get's a warning that 'a is constraint to obj. I Don't want to figure out if IEnumerable is implemented but IEnumerabl<>.

Any one know's the solution and btw feel free to comment on the code above as well. It's my first non-trivial F# program

© Stack Overflow or respective owner

Related posts about F#

Related posts about reflection