zoukankan      html  css  js  c++  java
  • 2019-8-31-dotnet-特性-DynamicallyInvokable-是用来做什么的

    title author date CreateTime categories
    dotnet 特性 DynamicallyInvokable 是用来做什么的
    lindexi
    2019-08-31 16:55:58 +0800
    2019-7-27 11:7:34 +0800
    dotnet

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

    堆栈 网找到了以下描述

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

     // 每个神奇的(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

  • 相关阅读:
    设计模式之工厂模式 练习
    c++智能指针(1)
    记录下 UTF6 GBK 转换函数
    ip白名单 通过* ? 检测IP匹配 轻量级
    stl学习记录(2)
    boost 学习(1)
    stl string 小练习
    stl string 使用指定的分隔符分割成数个子字符串
    [open source] skinbuilder发布
    Builder模式实例分析(C语言版)
  • 原文地址:https://www.cnblogs.com/lindexi/p/12085822.html
Copyright © 2011-2022 走看看