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?
OK so in the course of typing the question and doing some more experimenting to make sure I wasn’t asking something really stupid, I realized an easy solution. Posting here in case somebody else ever does something equally stupid. 😉
You can’t use the “is” keyword in your lambda expression, but you can use:
yielding:
hat-tip to Bob Vale for noting in the comments below that I should have been using .Any(…) rather than .Count(…) > 0