I have code that is structured like this:
class A {
void M() {}
}
delegate void B<T1,T2>(T1 key, T2 value);
And I'm using the ICorProfiler2 API to profile that code. Now, when method M is jitted, I get a FunctionID* pointer to its ID. I then do the following (heavily abbreviated):
mdToken functionToken = mdTypeDefNil;
mdTypeDef classToken = mdTypeDefNil;
IMetaDataImport* pMDImport = NULL;
profilerInfo->GetTokenAndMetaDataFromFunction(functionId,
IID_IMetaDataImport, (IUnknown**) &pMDImport, &functionToken);
pMDImport->GetMethodProps(functionToken, &classToken, functionName,
sizeof(functionName), 0, &methodAttr, &sigBlob, &sigSize, NULL,
NULL);
This gives me a TypeToken in the variable classToken.
I expected this to be the type in which M was declared in the source (a.k.a class A), but instead I receive delegate B's type token. So my question is: Am I doing something wrong or is my assumption that GetMethodProps returns the type token of M's definition type wrong?
Unfortunately, the documentation of GetMethodProps is not very helpful: https://msdn.microsoft.com/en-us/library/ms233163(v=vs.110).aspx
Edit: To clarify: I know that the wrong type is returned because I generated a file that contains all type/function tokens and the names they map to. I verified these tokens with ILSpy: they are correct. This mapping has also been tested numerous times in other applications and seems to work just fine, so I don't think this is the source of the problem.
So far, noone could provide an explanation as to why this is happening. All of my investigations lead me to believe that the profiler API sometimes simply reports the wrong type for a method.
Fortunately, the method tokens are unique within the entire assembly, so we now use these + the assembly name to identify each method uniquely and ignore the type tokens completely.