Arun Satyarth
Arun Satyarth

Reputation: 454

What is the purpose of MethodInfo.MetadataToken

What is the token normally used for? More importantly, if I have a MetadataToken, can I get back the MethodInfo object?

Upvotes: 13

Views: 5125

Answers (1)

vcsjones
vcsjones

Reputation: 141588

The metadata token is something that is part of the CIL specification for how data is organized. It's largely something you never need to care about.

More specifically, the metadata token is a tuple of a table identifier and a resource identifier.

You cannot rely on a metadata token to remain consistent between compiles, so I would not use it to identify a method.

You can use Module.ResolveMethod to convert a metadata token back to a MethodBase. MethodBase is the base type for MethodInfo. If the metadata token is for a method (as opposed to a constructor), then you should be able to cast it to a MethodInfo.

An assembly is made up of one or more modules, but practically an assembly only contains a single module - the C# and VB.NET compilers do not produce multi-module assemblies.

Upvotes: 11

Related Questions