前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >dotnet 特性 DynamicallyInvokable 是用来做什么的

dotnet 特性 DynamicallyInvokable 是用来做什么的

作者头像
林德熙
发布2022-08-07 13:22:16
9420
发布2022-08-07 13:22:16
举报
文章被收录于专栏:林德熙的博客林德熙的博客

我在 Linq 很多函数都看到 __DynamicallyInvokable 这个特性,这是一个没有官方文档的特性,也许是用来优化反射

堆栈 网找到了以下描述

这个 __DynamicallyInvokable 特性是没有官方文档的,好像是在 .NET Framework 4.5 的一个优化添加的特性,这个特性看起来是在优化反射缓存的值,可以让随后的反射代码运行更快。从源代码里面的 System.Reflection.Assembly.cs 文件可以看到以下描述

代码语言:javascript
复制
 // 每个神奇的(blessed)的 API 都会添加 "__DynamicallyInvokableAttribute" 注释
 // Each blessed API will be annotated with a "__DynamicallyInvokableAttribute".
 // 这个 "__DynamicallyInvokableAttribute" 特性类是在他自己的程序集定义
 // This "__DynamicallyInvokableAttribute" is a type defined in its own assembly.
 // 所以他的构造函数总是一个 MethodDef 同时是 TypeDef 类型
 // So the ctor is always a MethodDef and the type a TypeDef.
 // 我们缓存此构造的 MethodDef 标记以便更快地进行自定义属性查找
 // We cache this ctor MethodDef token for faster custom attribute lookup.
 // 如果在程序集里面不包含这个特性,那么意味着这个程序集不存在任何神奇的(blessed)的 API 方法
 // 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;
 }

源代码请看 Assembly.cs

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档