bianconero
bianconero

Reputation: 225

Get implemented interfaces of a type using Mono.Cecil

Sorry if the question sounds trivial, I've read many discussions about that problem, but I'm not 100 percent sure if I got it correctly:

I want to get all interfaces, which are implemented by a class- then I can use following code:

foreach (TypeDefinition typeItem in currentAssembly.MainModule.Types)
{
  // if the class has interfaces, get them
  Mono.Collections.Generic.Collection<TypeReference> interfaceList = typeItem.Interfaces;
}

So the variable "interfaceList" should now contain all interfaces, which "typeItem" implements- it does not matter if the interfaces are implemented explicitly or implicitly?

Thanks in advance for your answers!

Upvotes: 0

Views: 946

Answers (1)

Piotr Stapp
Piotr Stapp

Reputation: 19830

To find out if above code is working you need to create simple assemblies with following test cases. Then code some unit test and you are done.

Test cases:

  1. Class without interface - array should be empty
  2. Class with one interface - array should have one element
  3. Class with multiple interfaces
  4. Class which inherits class from point 1
  5. Class which inherits class from point 3
  6. Class which inherits class from point 2 and implements one more interface

If points 5 and 6 doesn't show correct number of interfaces you can sum up "this" class interfaces and "all-parents" class interfaces

Upvotes: 1

Related Questions