Collect types that implement a specific non-cast interface?

Basically, I want to know if all the types in a particular namespace implements a particular interface, for example IEnumerable

.

I know I can:

foreach type ...
    if type is IEnumerable
        ...

      

But I don't want the type to just be queried if it implements the interface, because the cast will be thrown anyway.

0


source to share


3 answers


Operator use is not cast, this is a test that sounds like you want. The fill type in C # is done either with the forced casting operator (name?), Like this:

((IEnumerable)someCollection)

      



or a safe casting operator:

someCollection as IEnumerable

      

+6


source


Assuming I read it correctly, you want a list of types in an enum that implement a specific interface or supertype. Perhaps Enumerable.OfType (this IEnumerable source) is helpful?



var enumerables = myListOfThingsToCheck.OfType<IEnumerable>();

      

+3


source


I think this will work if you prefer using the lambda syntax.

var enumerables=Assembly.GetExecutingAssembly().GetTypes().Where(t => t.Namespace == "YOUR NAMESPACE HERE").OfType<IEnumerable>();

      

+1


source







All Articles