How do I enumerate a list of interfaces that are directly defined on an inheriting class/interface?
- by Jordan
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.