none
How to get method parameter's signature? RRS feed

  • Question

  • Hi, I have to save  method parameter's signature to do sth.

    I can parser the signature from IMetaDataImport::GetMethodProps and get method parameter's signature rightly, but it's dirty because I have to split parameters and save each parameter's beginning and ending. So I try another way below, but it's not work.

    I use IMetaDataImport::GetParamForMethodIndex to do get all parameters' mdParamDef, then use IMetaDataImport::GetSigFromToken with mdParamDef to get signature.

    IMetaDataImport::GetSigFromToken returns 0x80131124 for some mdParamDef, and what's the meaning?

    I parser the signature return by IMetaDataImport::GetSigFromToken. The result of parameter's type all are ELEMENT_TYPE_U2. Obviously, it is not right. 

    Can you tell me why? Thanks a lot! 


    Wednesday, September 21, 2016 10:17 AM

Answers

  • Not all parameters will have an entry in the Params table, and hence not have a Params metadata token (if the parameter has no name, attributes, default value, etc. then it tends not to have a Param record). Though, I would have expected this to result in GetParamForMethodIndex to return CLDB_E_INDEX_NOTFOUND (0x80131124) rather than GetSigFromToken.

    I would be surprised if passing a parameter token into GetSigFromToken actually worked at all. The metadata format does not store the parameter signatures separately from method signature, so if it were supported, it would have to parse the method signature to return the subset ... which would seem very "out of character" for IMetaDataImport.

    You said that it's always returning ELEMENT_TYPE_U2 for the parameters, is the signature length returned in pcbSig actually 1? I have a sneaking suspicion that GetSigFromToken is not verifying the metadata token type (stored in the most significant byte) and just assuming that the row number (the remaining 3 bytes) indexes into the StandAloneSig table. In which case your parameters would almost certainly be pulling out local variable signatures. Local variable signatures always start with LOCAL_SIG, which happens to have the same value as ELEMENT_TYPE_U2 (0x07).

    Circling back to the first paragraph: I suppose GetSigFromToken could be returning CLB_E_INDEX_NOTFOUND if the Param token you pass it happens to have a row number beyond the length of the StandAloneSig table.

    Long story short, parsing the method signature to get parameter types isn't so much "dirty" as "the way".

    • Proposed as answer by DotNet WangModerator Thursday, September 22, 2016 2:25 AM
    • Marked as answer by IMMark Thursday, September 22, 2016 7:36 AM
    Wednesday, September 21, 2016 1:50 PM

All replies

  • Not all parameters will have an entry in the Params table, and hence not have a Params metadata token (if the parameter has no name, attributes, default value, etc. then it tends not to have a Param record). Though, I would have expected this to result in GetParamForMethodIndex to return CLDB_E_INDEX_NOTFOUND (0x80131124) rather than GetSigFromToken.

    I would be surprised if passing a parameter token into GetSigFromToken actually worked at all. The metadata format does not store the parameter signatures separately from method signature, so if it were supported, it would have to parse the method signature to return the subset ... which would seem very "out of character" for IMetaDataImport.

    You said that it's always returning ELEMENT_TYPE_U2 for the parameters, is the signature length returned in pcbSig actually 1? I have a sneaking suspicion that GetSigFromToken is not verifying the metadata token type (stored in the most significant byte) and just assuming that the row number (the remaining 3 bytes) indexes into the StandAloneSig table. In which case your parameters would almost certainly be pulling out local variable signatures. Local variable signatures always start with LOCAL_SIG, which happens to have the same value as ELEMENT_TYPE_U2 (0x07).

    Circling back to the first paragraph: I suppose GetSigFromToken could be returning CLB_E_INDEX_NOTFOUND if the Param token you pass it happens to have a row number beyond the length of the StandAloneSig table.

    Long story short, parsing the method signature to get parameter types isn't so much "dirty" as "the way".

    • Proposed as answer by DotNet WangModerator Thursday, September 22, 2016 2:25 AM
    • Marked as answer by IMMark Thursday, September 22, 2016 7:36 AM
    Wednesday, September 21, 2016 1:50 PM
  • >You said that it's always returning ELEMENT_TYPE_U2 for the parameters, is the signature length returned in pcbSig actually 1?

    No, I regard it as parameter signature blob wrongly before. Its pcbSig is more than 1. And I parser it rightly when I treat it as LocalVarSig if GetSigFromToken return S_OK not CLDB_E_INDEX_NOTFOUND. 

    I think I get it. Maybe GetSigFromToken should improve its verifying or add some remark.

    Thank you and have a nice day.


    • Edited by IMMark Thursday, September 22, 2016 7:42 AM nothing
    Thursday, September 22, 2016 7:35 AM