Markus Johnsson
Markus Johnsson

Reputation: 4019

How to get a System.Reflection.Metadata.MethodDefintion from System.Reflection.MethodBase.MetadataToken?

I have created a System.Reflection.Metadata.MetadataReader for an .NET assembly:

var asm = new PEReader(peFile);
var asmReader = asm.GetMetadataReader();

I can loop over all MethodDefinitions, but how do I find a specific MethodDefinition, for a given instance of System.Reflection.MethodBase?

Can I use MethodBase.MetadataToken to lookup the correct MethodDefinition?

I also want to lookup the corresponding MethodDebugInformation from the Portable PDB; but that should be easy after finding the MethodDefinition.

Upvotes: 2

Views: 1108

Answers (1)

Markus Johnsson
Markus Johnsson

Reputation: 4019

So, the solution is simple enough, System.Reflection.Metadata.Ecma335 contains a MetadataTokens class with methods to create handles, which can then be used to lookup the MethodDefinition:

var handle = (MethodDefinitionHandle)MetadataTokens.Handle(metadataToken);
var md = asmReader.GetMethodDefinition(handle);

Upvotes: 4

Related Questions