IL3003:“RequiresAssemblyFilesAttribute”批注在所有接口实现或替代中必须匹配

规则 ID IL3003
类别 SingleFile
修复是中断修复还是非中断修复 非中断性

原因

将应用作为单个文件发布(例如,将项目中的 PublishSingleFile 属性设置为 true)时,接口实现或其成员没有匹配的 [RequiresAssemblyFiles] 批注的派生类可能会导致调用具有与单文件不兼容属性的成员。 有四种可能的情况可以生成警告:

  • 基类的成员具有属性,但派生类的替代成员不具有属性:

    public class Base
    {
        [RequiresAssemblyFiles]
        public virtual void TestMethod() {}
    }
    public class Derived : Base
    {
        // IL3003: Base member 'Base.TestMethod' with 'RequiresAssemblyFilesAttribute' has a derived member 'Derived.TestMethod()' without 'RequiresAssemblyFilesAttribute'. For all interfaces and overrides the implementation attribute must match the definition attribute.
        public override void TestMethod() {}
    }
    
  • 基类的成员没有属性,但派生类的替代成员具有属性:

    public class Base
    {
        public virtual void TestMethod() {}
    }
    public class Derived : Base
    {
        // IL3003: Member 'Derived.TestMethod()' with 'RequiresAssemblyFilesAttribute' overrides base member 'Base.TestMethod()' without 'RequiresAssemblyFilesAttribute'. For all interfaces and overrides the implementation attribute must match the definition attribute.
        [RequiresAssemblyFiles]
        public override void TestMethod() {}
    }
    
  • 接口成员具有属性,但其实现没有属性:

    interface IRAF
    {
        [RequiresAssemblyFiles]
        void TestMethod();
    }
    class Implementation : IRAF
    {
        // IL3003: Interface member 'IRAF.TestMethod()' with 'RequiresAssemblyFilesAttribute' has an implementation member 'Implementation.TestMethod()' without 'RequiresAssemblyFilesAttribute'. For all interfaces and overrides the implementation attribute must match the definition attribute.
        public void TestMethod () { }
    }
    
  • 接口成员没有属性,但其实现具有属性:

    interface INoRAF
    {
        void TestMethod();
    }
    class Implementation : INoRAF
    {
        [RequiresAssemblyFiles]
        // IL3003: Member 'Implementation.TestMethod()' with 'RequiresAssemblyFilesAttribute' implements interface member 'INoRAF.TestMethod()' without 'RequiresAssemblyFilesAttribute'. For all interfaces and overrides the implementation attribute must match the definition attribute.
        public void TestMethod () { }
    }
    

如何解决冲突

对于所有接口和替代,实现都必须与“RequiresAssemblyFilesAttribute”属性在存在或缺失方面的定义匹配。 要么这两个成员都包含属性,要么都不包含属性。 在接口/基类成员或实现类/派生类成员上删除或添加属性,使该属性要么都在/要么都不在这两个成员上。

何时禁止显示警告

不应禁止显示此警告。