Looking stuff up by its metadata tokens: confusing results

125 views
Skip to first unread message

Greg Rosenbaum

unread,
Sep 7, 2015, 2:22:21 PM9/7/15
to mono-cecil
I'm using PEVerify to check the assembly I generate using Cecil is correct. Sometimes, it outputs a metadata token without an identifier to go with it. I want to add identifiers to the output by regex replace and looking up the identifiers in Cecil (by using the same AssemblyDefinition that I wrote to file). The problem is that when I use assembly.MainModule.LookupToken(new MetadataToken(the token uint I got from PEVerify)) to find the member with that token, I don't get the right member, or I get nothing. So I thought PEVerify somehow gives me the wrong token, but when I open the assembly using ILSpy, ildasm, or a similar tool, I see that it's the right metadata token for that member after all.

What am I doing wrong?

Greg Rosenbaum

unread,
Sep 7, 2015, 3:05:23 PM9/7/15
to mono-cecil
Okay, I actually figured out what was happening. The table that contains the metadata tokens of each element was incorrect, and I had to write the assembly to file and then read it again to get the correct table. I'd still like to know about the process, and if there is a better way to refresh the lookup table.

Also, I can't seem to look up properties by their metadata. It always returns null.

Jb Evain

unread,
Sep 8, 2015, 4:31:05 AM9/8/15
to mono-...@googlegroups.com
Hi Greg,

As you noted, the way LookupToken currently works is by reading data
from the Image.
When writing an assembly, Cecil is recompute the tokens. LookupToken
on a freshly written image probably won't work well, that's why you're
reading it again.

LookupToken currently supports the following tables:

TypeDef
TypeRef
TypeSpec
Field
Method
MemberRef
MethodSpec

As properties and events can't be referenced accross modules.

Jb
> --
> --
> --
> mono-cecil
> ---
> You received this message because you are subscribed to the Google Groups
> "mono-cecil" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mono-cecil+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages