首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >__DynamicallyInvokable属性的作用是什么?

__DynamicallyInvokable属性的作用是什么?
EN

Stack Overflow用户
提问于 2012-09-23 16:12:05
回答 2查看 25K关注 0票数 189

查看DotPeek中的System.Linq.Enumerable,我注意到一些方法带有[__DynamicallyInvokable]属性。

这个属性扮演了什么角色?它是由DotPeek添加的,还是扮演了另一个角色,也许是通知编译器如何最好地优化方法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-23 19:44:41

它没有文档记录,但看起来像是.NET 4.5中的优化之一。它似乎被用来填充反射类型信息缓存,使得公共框架类型上的后续反射代码运行得更快。在System.Reflection.Assembly.cs的参考源,RuntimeAssembly.Flags属性中有一个关于它的评论:

代码语言:javascript
运行
复制
 // Each blessed API will be annotated with a "__DynamicallyInvokableAttribute".
 // This "__DynamicallyInvokableAttribute" is a type defined in its own assembly.
 // So the ctor is always a MethodDef and the type a TypeDef.
 // We cache this ctor MethodDef token for faster custom attribute lookup.
 // If this attribute type doesn't exist in the assembly, it means the assembly
 // doesn't contain any blessed APIs.
 Type invocableAttribute = GetType("__DynamicallyInvokableAttribute", false);
 if (invocableAttribute != null)
 {
     Contract.Assert(((MetadataToken)invocableAttribute.MetadataToken).IsTypeDef);

     ConstructorInfo ctor = invocableAttribute.GetConstructor(Type.EmptyTypes);
     Contract.Assert(ctor != null);

     int token = ctor.MetadataToken;
     Contract.Assert(((MetadataToken)token).IsMethodDef);

     flags |= (ASSEMBLY_FLAGS)token & ASSEMBLY_FLAGS.ASSEMBLY_FLAGS_TOKEN_MASK;
 }

没有进一步的暗示一个“受祝福的API”可能意味着什么。尽管从上下文中可以清楚地看出,这只适用于框架本身中的类型。应该有额外的代码来检查应用于类型和方法的属性。不知道它位于何处,但考虑到它必须拥有所有.NET类型的视图才能尝试缓存,我只能想到Ngen.exe。

票数 145
EN

Stack Overflow用户

发布于 2013-10-17 18:06:15

我发现它被用在内部方法的Runtime*Info.IsNonW8PFrameworkAPI()套件中。将此属性放在成员上会使IsNonW8PFrameworkAPI()为其返回false,从而使该成员在WinRT应用程序中可用,并关闭The API '...' cannot be used on the current platform.异常。

如果探查器编写者希望在WinRT下访问框架程序集,则应该将此属性放在由其探查器发出的框架程序集中的成员上。

票数 26
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12550749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档