Read/Get TypeRef table from assembly metadata

2019-02-26 15:10发布

问题:

This is a follow-up question to THIS one:

To analyze an assembly (or the types it ueses) I would like to read the TypeRef table of such assembly.

I got the hint to use Mono.Cecil to do this, but I only found examples reading TypeDef information. Also browsing the source-code of cecil I only found internal classes which seems to me are responsible for reading the metadata, but I found no "public interface".

I also found THIS article which uses some COM-library to read metadata, but I couldn't figure out how to use that for my purpose either.

Can anyone tell me how I can read the TypeRef table (preferable using somthing like Mono.Cecil and not COM) of an assembly?

I am also happy if someone can point me in the right direction...

回答1:

Using Cecil, it's just a matter of accessing ModuleDefinition.GetTypeReferences(). For example:

var assembly = AssemblyDefinition.ReadAssembly(assemblyPath);

var typeReferences = assembly.MainModule.GetTypeReferences();