I have a class like the below. GetInterfaces() says
If the current Type represents a type
parameter in the definition of a
generic type or generic method, this
method searches the interface
constraints and any interfaces
inherited from class or interface
constraints.
Is it possible for me to not get any inherited interface? When i use GetInterfaces on ABC i only want to see DEF, not DEF and GHI.
interface DEF : GHI {...}
class ABC : DEF {...}
Firstly, the MSDN snippet you’ve posted doesn’t have anything to do with your actual question. It deals with when you have, for example, a generic type such as
class Foo<T> where T : IEnumerable, and you try callingGetInterfaceson the type-parameterT, for example throughtypeof(Foo<>).GetGenericArguments().Single().GetInterfaces().Secondly, the problem is slightly ill-specified. Note that when a class implements an interface, it must implement all of the interfaces ‘inherited’ by that interface. It’s simply a C# convenience feature that lets you omit the inherited interfaces in the class-declaration. In your example, it’s perfectly legal (and no different) to explicitly include the ‘inherited’
GHIinterface:I’ve assumed that what you really want to do is find a ‘minimal set’ of interfaces that ‘covers’ all of the type’s implemented interfaces. This results in a slightly simplified version of the Set cover problem.
Here’s one way to solve it, without any attempt whatsoever to be algorithmically efficient. The idea is to produce the minimal interface-set by filtering out those interfaces that are already implemented by other interfaces implemented by the type.
(
EDIT – Here’s a better way of doing the above:
)
For example, for
List<int>:Do note that this solution covers interface ‘hierarchies’ only (which is what you appear to want), not how they relate to the class’s class hierarchy. In particular, it pays no attention to where in a class’s hierarchy an interface was first implemented.
For example, let’s say we have:
Now if you try using the solution I’ve described to get the minimal interface-set for
Derived, you would getIBazas well asIBar. If you don’t wantIBar, you would have to go to more effort: eliminate interfaces implemented by base-classes. The easiest way to do this would be to remove from the minimal interface-set those interfaces implemented by the class’s immediate base-class, as is mentioned in @MikeEast’s answer.