CA2257:使用 'DynamicInterfaceCastableImplementationAttribute' 在介面上定義的成員應該是 'static'

屬性
規則識別碼 CA2257
職稱 在具有 'DynamicInterfaceCastableImplementationAttribute' 的介面上定義的成員應該是 'static'
類別 使用方式
修正是造成中斷還是不中斷 不中斷
在 .NET 10 中預設啟用 作為警告
適用語言 C# 與 Visual Basic

原因

介面成員未明確實作或進行static標示。

規則描述

由於實作的 IDynamicInterfaceCastable 型別可能不會在元資料中實作動態介面,呼叫非明確定義於此型別的實例介面成員,在執行時很可能會失敗。 為避免執行時錯誤,請標記新的介面成員 static

如何修正違規

標記介面成員 static

Example

[DynamicInterfaceCastableImplementation]
interface IExample
{
    // This method violates the rule.
    void BadMethod();

    // This method satisfies the rule.
    static void GoodMethod()
    {
        // ...
    }
}

抑制錯誤的時機

請勿隱藏此規則的警告。

另請參閱