One of the cases where a public method of an internal class might be accessible from outside the assembly is if the method implements interface methods or overrides virtual methods that are defined in a public base class.
Using IMetadataImport, how can find out if this is the case for a specific mdMethodDef?
Update: I'd also like to know how to do this in Mono.Cecil, as that might help me figure out how to do it in IMetaDataImport.
If I take this C# sample:
Here, the
Test
class successfully implements theITest
interface, as defined in C# specification (for example 13.4.2 Interface mapping)If you examine the result of this code in the compiled assembly (using a tool such as .NET Reflector or ILDASM), you will see this:
And... yes...there is nothing here in the assembly metadata that will relate the DoSomething method in Test to the DoSomething method in ITest.
In VB.NET, it's different, you will need to add an
Implements
keyword to make sure it compiles:As you see, with VB.NET, you need to explicitely relate the method in the class with the method in the interface, and if you analyse what IL has been created in the assembly in the VB.NET case, you'll find this:
So, with a VB-compiled assembly, the information is there, with a C#-compile assembly, it's not. It depends on the language. The CLR engine will in fact to the mapping at runtime.
If you can inject the assemblies in your process, this code can help you determine interface mapping:
But if you need to determine this only looking at metadata, you'll have to write that code yourself. It's not that easy, especially with generics. Also don't forget in C#, a public method can implicitely implement multiple interfaces.
A link that can help: Mono.Cecil something like Type.GetInterfaceMap?