通过


CA2256:在父接口中声明的所有成员必须在带有 DynamicInterfaceCastableImplementation 特性的接口中具有实现

属性
规则 ID CA2256
标题 声明在父接口中的所有成员必须在具有 DynamicInterfaceCastableImplementation 属性的接口中实现。
类别 使用情况
修复会引起中断还是不会引起中断 非中断
在 .NET 10 中默认启用 作为警告
适用的语言 C# 和 Visual Basic

原因

使用 DynamicInterfaceCastableImplementationAttribute 的接口有一个未实现的成员。

规则说明

被赋予特性DynamicInterfaceCastableImplementationAttribute的类型作为实现IDynamicInterfaceCastable类型的接口实现。 因此,它必须提供继承接口中定义的所有成员的实现,因为若非如此,实现 IDynamicInterfaceCastable 的类型将不会提供它们。

如何解决违规

实现未实现的接口成员。

Example

interface IParent
{
    void ParentMethod();
}

// This interface violates the rule.
[DynamicInterfaceCastableImplementation]
interface IBadChild : IParent
{
    static void ChildMethod()
    {
        // ...
    }
}

// This interface satisfies the rule.
[DynamicInterfaceCastableImplementation]
interface IGoodChild : IParent
{
    static void ChildMethod()
    {
        // ...
    }

    void IParent.ParentMethod()
    {
        // ...
    }
}

何时抑制错误

不要抑制此规则发出的警告。

另请参阅