Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I determine interface requirements/dependencies/inheritance programatically?

Is there a way (reflection or otherwise) to programatically determine that the IList interface requires ICollection, which in turn requires IEnumerable?

I'm working on a reflection library and came across a scenario where I could use this information, but found no way to obtain it. Both the compiler and the IDE (obviously) know the relation, so it must be available somehow.

I'm hoping for suggestions that don't involve IL or source parsing, neither of which really is an option for my use case.

like image 473
Morten Mertner Avatar asked Oct 21 '11 23:10

Morten Mertner


2 Answers

You can use Type.GetInterfaces to discover this information.

like image 80
Jon Avatar answered Sep 30 '22 04:09

Jon


Examples below in powershell:

PS C:\> [collections.ilist].getinterfaces()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    ICollection
True     False    IEnumerable

The equivalent in c# would be: typeof(IList).GetInterfaces().

like image 43
x0n Avatar answered Sep 30 '22 04:09

x0n