简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM