191

System.Linq.EnumerableDotPeek を調べてみると、いくつかのメソッドが属性でフレーバーされていることに気付きました[__DynamicallyInvokable]

この属性はどのような役割を果たしますか? それは DotPeek によって追加されたものですか、それともメソッドを最適化する最善の方法をコンパイラに通知するなど、別の役割を果たしているのでしょうか?

4

2 に答える 2

145

文書化されていませんが、.NET 4.5 の最適化の 1 つと思われます。これは、リフレクション タイプの情報キャッシュを準備するために使用されているようで、共通のフレームワーク タイプで後続のリフレクション コードをより高速に実行できるようにします。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 しか考えられません。

于 2012-09-23T11:44:41.017 に答える