Reputation: 331102
Basically I wanna know if all the types in a particular namespace implements a particular interface like IEnumerable
.
I know I can do:
foreach type ...
if type is IEnumerable
...
But I don't wanna cast the type just to query if it implements an interface, because the cast will be thrown away either way.
Upvotes: 0
Views: 178
Reputation: 2718
I think this will work also if you'd rather use lambda syntax.
var enumerables=Assembly.GetExecutingAssembly().GetTypes().Where(t => t.Namespace == "YOUR NAMESPACE HERE").OfType<IEnumerable>();
Upvotes: 1
Reputation: 116498
Assuming I've read you correctly, you want a list of types in an enumeration that implement an particular interface or supertype. Perhaps Enumerable.OfType(this IEnumerable source) is useful?
var enumerables = myListOfThingsToCheck.OfType<IEnumerable>();
Upvotes: 3
Reputation: 12975
Using the is operator is not a cast, it is a test, which sounds like what you want. Type casting in C# is done either using the forced casting operator (name?) like so:
((IEnumerable)someCollection)
or the safe casting operator:
someCollection as IEnumerable
Upvotes: 6