原因
當你將應用程式發佈為 Native AOT(在專案中將PublishAot屬性設定為true)時,呼叫使用RequiresDynamicCodeAttribute屬性註解的成員可能會導致執行時例外狀況。 標註此屬性的成員可能需要在執行時動態建立新程式碼,而 Native 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。