How do I enumerate a list of interfaces that are directly defined on an inheriting class/interface?

Posted by Jordan on Stack Overflow See other posts from Stack Overflow or by Jordan
Published on 2010-03-25T19:35:19Z Indexed on 2010/03/25 19:43 UTC
Read the original article Hit count: 377

Filed under:
|
|
|

Given the following C# class:

public class Foo : IEnumerable<int>
{
    // implementation of Foo and all its inherited interfaces
}

I want a method like the following that doesn't fail on the assertions:

public void SomeMethod()
{
   // This doesn't work
   Type[] interfaces = typeof(Foo).GetInterfaces();

   Debug.Assert(interfaces != null);
   Debug.Assert(interfaces.Length == 1);
   Debug.Assert(interfaces[0] == typeof(IEnumerable<int>));
}

Can someone help by fixing this method so the assertions don't fail?

Calling typeof(Foo).GetInterfaces() doesn't work because it returns the entire interface hierarchy (i.e. interfaces variable contains IEnumerable<int> and IEnumerable), not just the top level.

© Stack Overflow or respective owner

Related posts about c#

Related posts about classes