IL3050:作为本机 AOT 发布时,避免调用使用“RequiresDynamicCodeAttribute”注释的成员
原因
在将应用作为本机 AOT 发布(通过将 PublishAot
属性设置为 true
)时,调用使用属性 RequiresDynamicCodeAttribute
注释的成员可能会导致运行时出现异常。 使用此属性注释的成员可能需要能够在运行时动态创建新代码,而本机 AOT 发布模型未提供在运行时生成本机代码的方法。
规则说明
RequiresDynamicCodeAttribute 表示成员引用了可能需要在运行时生成代码的代码。
示例
// 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 { }
如何解决冲突
使用属性“RequiresDynamicCodeAttribute
”注释的成员获得一条消息,该消息可为作为本地 AOT 发布的用户提供有用的信息。 请考虑根据属性的消息调整现有代码,或者删除有冲突的调用。
在特定模式中调用时,使用 RequiresDynamicCodeAttribute
进行批注的某些 API 不会触发警告。 有关详细信息,请参阅标记为 RequiresDynamicCode 的内部 API。