How can I check the type of an object against a list of types?

Posted by RookieRick on Stack Overflow See other posts from Stack Overflow or by RookieRick
Published on 2012-10-26T22:29:25Z Indexed on 2012/10/26 23:00 UTC
Read the original article Hit count: 196

Filed under:
|
|

Given a collection

IEnumerable<Type> supportedTypes

What's the best way to check whether a given object is one of those types (or a derived type)?

My first instinct was to do something like:

// object target is a parameter passed to the method in which I'm doing this.
if (supportedTypes.Count( supportedType => target is supportedType ) > 0)
{
    // Yay my object is of a supported type!!!
}

..but that doesn't seem to be working. Can I not use the "is" keyword in a lambda expression like this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ