IL3050: Avoid calling members annotated with 'RequiresDynamicCodeAttribute' when publishing as Native AOT
Cause
When you publish an app as Native AOT (by setting the PublishAot
property to true
in a project), calling members annotated with the RequiresDynamicCodeAttribute
attribute might result in exceptions at run time. Members annotated with this attribute might require ability to dynamically create new code at run time, and Native AOT publishing model doesn't provide a way to generate native code at run time.
Rule description
RequiresDynamicCodeAttribute indicates that the member references code that might require code generation at run time.
Example
// AOT analysis warning IL3050: Program.<Main>$(String[]): Using member 'System.Type.MakeGenericType(Type[])'
// which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for
// this instantiation might not be available at runtime.
typeof(Generic<>).MakeGenericType(unknownType);
class Generic<T> { }
struct SomeStruct { }
How to fix violations
Members annotated with the RequiresDynamicCodeAttribute
attribute have a message that provides useful information to users who are publishing as Native AOT. Consider adapting existing code to the attribute's message or removing the violating call.
Some APIs annotated with RequiresDynamicCodeAttribute
don't trigger a warning when called in a specific pattern. For more information, see Intrinsic APIs marked RequiresDynamicCode.