none
Why typeof(int).MetadataToken returns TypeDef but not TypeRef token? RRS feed

  • Question

  • Hello

    Have two line console app which executes

    int result = typeof(int).MetadataToken;

    and prints result. It appears that results value is TypeDef(0x02) token, but not TypeRef(0x01 as i was expecting). It would be great if some could help me to understand:

    - why it's like that?

    - how to get original token. (in this case TypeRef) from current scope in easy way?

    Thx for your help!


    Regards


    • Edited by democls Friday, June 28, 2013 6:44 PM
    Friday, June 28, 2013 6:44 PM

Answers

  • "but not TypeRef(0x01 as i was expecting"

    And why would you expect a TypeRef?

    typeof(x) returns a Type object and there's only one Type object for each type. As a result the metadata token of the Type object must be the TypeDef, it can't be TypeRef because that one is different from module to module.

    "how to get original token. (in this case TypeRef) from current scope in easy way?"

    I don't think there's a way to do that with System.Reflection. In general reflection is concerned only with defs, refs are only "interesting" to compilers/decompilers etc. What are you trying to do anyway?

    "A value which, in combination with Module, uniquely identifies a metadata element. But executing: 'typeof(Program).Module.MetadataToken' will return same value as 'typeof(int).Module.MetadataToken' would do"

    So? The documentation you quote says "Module", not "Module.MetadataToken".

    "which makes it impossible to identify to which module this type belongs to."

    It belongs to the module returned by the Module property.

    "Besides it's kind of strange to have two different modules (ConsoleApplication1 and mscorlib) with same metadata token returned by reflection..."

    Why? Those 2 modules are in 2 different assemblies and metadata token are not unique across assemblies.

    • Marked as answer by democls Friday, June 28, 2013 10:04 PM
    Friday, June 28, 2013 8:41 PM
    Moderator

All replies

  • forgot to mention: as per MSDN for MemberInfo.MetadataToken PropertyA value which, in combination with Module, uniquely identifies a metadata element.

    But executing: 'typeof(Program).Module.MetadataToken' will return same value as 'typeof(int).Module.MetadataToken' would do, which makes it impossible to identify to which module this type belongs to. Besides it's kind of strange to have two different modules (ConsoleApplication1 and mscorlib) with same metadata token returned by reflection...

    I must be missing something simple here, right?


    Regards



    • Edited by democls Friday, June 28, 2013 6:57 PM fix typo
    Friday, June 28, 2013 6:54 PM
  • "but not TypeRef(0x01 as i was expecting"

    And why would you expect a TypeRef?

    typeof(x) returns a Type object and there's only one Type object for each type. As a result the metadata token of the Type object must be the TypeDef, it can't be TypeRef because that one is different from module to module.

    "how to get original token. (in this case TypeRef) from current scope in easy way?"

    I don't think there's a way to do that with System.Reflection. In general reflection is concerned only with defs, refs are only "interesting" to compilers/decompilers etc. What are you trying to do anyway?

    "A value which, in combination with Module, uniquely identifies a metadata element. But executing: 'typeof(Program).Module.MetadataToken' will return same value as 'typeof(int).Module.MetadataToken' would do"

    So? The documentation you quote says "Module", not "Module.MetadataToken".

    "which makes it impossible to identify to which module this type belongs to."

    It belongs to the module returned by the Module property.

    "Besides it's kind of strange to have two different modules (ConsoleApplication1 and mscorlib) with same metadata token returned by reflection..."

    Why? Those 2 modules are in 2 different assemblies and metadata token are not unique across assemblies.

    • Marked as answer by democls Friday, June 28, 2013 10:04 PM
    Friday, June 28, 2013 8:41 PM
    Moderator
  • Hi Mike and thx for your reply.

    Sounds like you answered my question. Thx a lot for your help! 


    Regards

    Friday, June 28, 2013 10:04 PM