Search code examples
cil

CIL hex code to call method in another assembly


For example, I'm writing code in assembly A, and the method I want to call is in assembly B at 0x06000DF2. Here is the hex dnSpy create for me 6F8701000A, but I don't know how it's calculated. Please explain to me. Thank you!


Solution

  • The first byte (6F) indicates that it is the callvirt instruction, the remaining 4 bytes is the metadata token for the method little endian byte order.

    callvirt 0x0A000187
    

    The metadata token is a reference to a particular row in a particular table in the metadata of the current module (the module that contains the IL). The high order byte indicates the type of token (and hence, which metadata table to look in), while the remaining 3 bytes indicates the row number within the table. 0x0A indicates that the target row is in the MemberRef table and the referenced record will provide the details necessary to find the correct member.

    The MemberRef table is described in ECMA-335 Partition II, section 22.25.