Using unmanaged code, how can I find a type that has a given custom attribute assigned to it?
I had thought I could enumerate the types using IMetaDataImport.EnumTypeDefs and for each of the tokens returned, call IMetaDataImport.EnumCustomAttributes.
This works, in so much as I get an array of mdCustomAttribute tokens. Using these tokens I can get a metadata token representing the Type of the returned custom attribute, by calling IMetaDataImport.GetCustomAttributeProps.
Comparing my results against ILDASM, I can see that this matches the "CustomAttribute Type" that ILDASM reports. However, I cannot work out how to determine the "CustomAttributeName" that ILDASM reports. This is what I really want!
While I would be interested in knowing how to get the CustomAttributeName, I would settle for an alternate approach to s开发者_JAVA百科olving the problem.
Sorry, this was a case of RTFM. The attribute returned by GetCustomAttributeProps
is not a typedef token, but a mdMethodDef
or mdMemberRef
token.
For mdMethodDef tokens, you use IMetaDataImport.GetMethodProps
to find the typeDef token and for mdMemberRefs, it is a slightly longer path, but you start with IMetaDataImport.GetMemberRefProps
.
The moral of the story is to pay attention to what sort of tokens these functions are returning!
精彩评论