IL3003 'RequiresAssemblyFilesAttribute' 標示必須符合所有介面實作或覆寫

規則識別碼 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' 屬性是否存在或不存在的定義。 兩個成員都包含屬性,或兩者都不包含屬性。 移除或新增介面/基底類別成員上的屬性,或實作/衍生類別成員,以讓屬性同時或不同時在兩者顯示。

隱藏警告的時機

您不應該隱藏這個警告。