什么是对__DynamicallyInvokable属性? [英] What is the __DynamicallyInvokable attribute for?

查看:2980
本文介绍了什么是对__DynamicallyInvokable属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

综观 System.Linq.Enumerable 在DotPeek我注意到,一些方法与调味一个 [__ DynamicallyInvokable] 属性。

Looking through System.Linq.Enumerable in DotPeek I notice that some methods are flavoured with a [__DynamicallyInvokable] attribute.

什么作用这个属性玩?它是由东西添加DotPeek或者它扮演另外一个角色,也许是通知编译器就如何最好地优化方法?

What role does this attribute play? Is it something added by DotPeek or does it play another role, perhaps informing the compiler on how best to optimise the methods?

推荐答案

这是无证的,但它看起来像在.NET 4.5的优化之一。这似乎是用来引发反射类型信息的高速缓存,使得后续的反射code在共同的框架类型的运行速度更快。有对System.Reflection.Assembly.cs的参考源关于它的评论,RuntimeAssembly.Flags属性:

It is undocumented, but it looks like one of the optimizations in .NET 4.5. It appears to be used to prime the reflection type info cache, making subsequent reflection code on common framework types run faster. There's a comment about it in the Reference Source for System.Reflection.Assembly.cs, RuntimeAssembly.Flags property:

 // 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可能意味着。虽然它是从上下文清楚,这将仅在框架本身类型工作。应该有额外的code的地方,检查应用类型和方法的属性。不知道在哪里位于,但考虑到它必须要有所有的.NET类型,以便在有缓存的一个镜头,我只能认为Ngen.exe的。

Without further hints what a "blessed API" might mean. Although it is clear from the context that this will only work on types in the framework itself. There ought to be additional code somewhere that checks the attribute applied to types and methods. No idea where that is located, but given that it would have to need to have a view of all .NET types to have a shot at caching, I can only think of Ngen.exe.

这篇关于什么是对__DynamicallyInvokable属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆