IL3058:引用的程序集未进行 AOT 兼容性批注

原因

项目的<VerifyReferenceAotCompatibility>true</VerifyReferenceAotCompatibility>已设置,但一个或多个引用的程序集未将程序集元数据属性设置为true (IsAotCompatible)。

规则说明

使用本机 AOT <PublishAot>true</PublishAot> 发布或将项目标记为与 AOT 兼容的 <IsAotCompatible>true</IsAotCompatible>时,可以选择启用验证,确认所有引用的程序集也针对 AOT 兼容性进行批注。 这有助于确保项目中的所有依赖项都被标注为 AOT 兼容。

若要启用此验证,请将 VerifyReferenceAotCompatibility 属性设置为 true 项目文件中:

<PropertyGroup>
  <PublishAot>true</PublishAot>
  <VerifyReferenceAotCompatibility>true</VerifyReferenceAotCompatibility>
</PropertyGroup>

启用此属性后,分析器会检查所有引用的程序集是否均已使用 <IsAotCompatible>true</IsAotCompatible> 生成,这样会将程序集级属性 [assembly: AssemblyMetadata("IsAotCompatible", "True")] 添加到这些程序集。

Example

// Assembly reference: MyLibrary.dll (built without <IsAotCompatible>true</IsAotCompatible>)

public class Program
{
    public static void Main()
    {
        var obj = new MyLibrary.SomeClass();
    }
}
warning IL3058: Referenced assembly 'MyLibrary' is not built with `<IsAotCompatible>true</IsAotCompatible>` and may not be compatible with AOT.

如何修复违规行为

可以使用多个选项来修复此警告:

  • 更新要生成的<IsAotCompatible>true</IsAotCompatible>。 如果控制库源代码,则这是首选方法。 该 IsAotCompatible 属性将程序集标记为与本机 AOT 兼容,并启用特定于 AOT 的分析。
  • 在项目文件中设置禁用验证,如果您确认该库即使没有该属性也能正常工作于本机 AOT 中。

另请参阅