Question

Some .NET reflection methods (for example, Module.ResolveField) accept so-called tokens as parameters. These tokens are just Int32 numbers. I've seen only positive tokens in disassembler, but, generally speaking, integer numbers can be negative or zero too. Is it safe to assume that CLR tokens are only positive?

Was it helpful?

Solution

They're actually meant to be unsigned numbers. From MS Partition II, section 22 (taken from this page):

Metadata items (records in the metadata tables) are addressed by metadata tokens. Uncoded metadata tokens are 4-byte unsigned integers, which contain the metadata table index in the most significant byte and a 1-based record index in the three least-significant bytes

Given that no metadata table index has its highest bit set, there's no concern that in converting an unsigned int to an int, you'll produce overflow or a negative number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top