什么是__DynamicallyInvokable属性?

在DotPeek中查看System.Linq.Enumerable我注意到一些方法调用了[__DynamicallyInvokable]属性。

这个属性起什么作用? 是由DotPeek添加的东西还是它扮演了另一个angular色,也许告诉编译器如何最好地优化方法?

它没有文档,但看起来像.NET 4.5中的优化之一。 它似乎被用来填充reflectiontypes的信息caching,使后续的通用框架types的reflection代码运行得更快。 在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”可能意味着什么。 虽然从上下文中可以清楚地看出,这只会在框架本身的types上起作用。 应该有额外的代码检查应用于types和方法的属性。 不知道它在哪里,但鉴于它需要有一个所有的.NETtypes的caching,我只能想到Ngen.exe。

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

Profiler编写者应该把这个属性放在由探查器发送到框架程序集的成员上,如果他们想在WinRT下访问它们的话。