查看DotPeek中的System.Linq.Enumerable
,我注意到一些方法带有[__DynamicallyInvokable]
属性。
这个属性扮演了什么角色?它是由DotPeek添加的,还是扮演了另一个角色,也许是通知编译器如何最好地优化方法?
发布于 2012-09-23 19:44:41
它没有文档记录,但看起来像是.NET 4.5中的优化之一。它似乎被用来填充反射类型信息缓存,使得公共框架类型上的后续反射代码运行得更快。在System.Reflection.Assembly.cs的参考源,RuntimeAssembly.Flags属性中有一个关于它的评论:
// 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。
发布于 2013-10-17 18:06:15
我发现它被用在内部方法的Runtime*Info.IsNonW8PFrameworkAPI()
套件中。将此属性放在成员上会使IsNonW8PFrameworkAPI()为其返回false
,从而使该成员在WinRT应用程序中可用,并关闭The API '...' cannot be used on the current platform.
异常。
如果探查器编写者希望在WinRT下访问框架程序集,则应该将此属性放在由其探查器发出的框架程序集中的成员上。
https://stackoverflow.com/questions/12550749
复制相似问题