Search code examples
c#dynamic-invoke

What is the __DynamicallyInvokable attribute for?


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

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?


Solution

  • 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;
     }
    

    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.