使用非托管代码,如何找到分配有给定自定义属性的类型?
我原以为我可以使用 IMetaDataImport.EnumTypeDefs 枚举类型并且 对于返回的每个标记,调用 IMetaDataImport.EnumCustomAttributes。
这是有效的,因为我得到了一组 mdCustomAttribute 令牌。使用这些标记,我可以通过调用 IMetaDataImport.GetCustomAttributeProps 来获取表示返回的自定义属性的类型的元数据标记。
将我的结果与 ILDASM 进行比较,我可以看到这与 ILDASM 报告的“CustomAttribute Type”相匹配。但是,我无法弄清楚如何确定 ILDASM 报告的“CustomAttributeName”。这才是我真正想要的!
虽然我有兴趣了解如何获取 CustomAttributeName,但我会选择另一种方法来解决问题。
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 solving the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,这是 RTFM 的案例。
GetCustomAttributeProps
返回的属性不是 typedef 令牌,而是mdMethodDef
或mdMemberRef
令牌。对于 mdMethodDef 令牌,您可以使用
IMetaDataImport.GetMethodProps
查找 typeDef 令牌,对于 mdMemberRefs,这是一个稍长的路径,但您可以从IMetaDataImport.GetMemberRefProps
开始。这个故事的寓意是要注意这些函数返回的令牌类型!
Sorry, this was a case of RTFM. The attribute returned by
GetCustomAttributeProps
is not a typedef token, but amdMethodDef
ormdMemberRef
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 withIMetaDataImport.GetMemberRefProps
.The moral of the story is to pay attention to what sort of tokens these functions are returning!