Skip to content

Document when warning CA2256 may be suppressed. - #56423

Open
teo-tsirpanis wants to merge 1 commit into
dotnet:mainfrom
teo-tsirpanis:patch-1
Open

teo-tsirpanis wants to merge 1 commit into
dotnet:mainfrom
teo-tsirpanis:patch-1

Conversation

@teo-tsirpanis

@teo-tsirpanis teo-tsirpanis commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The following snippet works despite the guidance that [DynamicInterfaceCastableImplementation] interfaces must provide implementations for all parent members.

using System;
using System.Runtime.InteropServices;

object x = new IdicObject();
((I1)x).X1();
((I2)x).X1();
((I2)x).X2();

sealed class IdicObject : IDynamicInterfaceCastable
{
    public RuntimeTypeHandle GetInterfaceImplementation(RuntimeTypeHandle interfaceType)
    {
        var t = Type.GetTypeFromHandle(interfaceType);
        return (t == typeof(I1) ? typeof(I1_Impl): typeof(I2_Impl)).TypeHandle;
    }

    public bool IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented)
    {
        var t = Type.GetTypeFromHandle(interfaceType);
        return t == typeof(I1) || t == typeof(I2);
    }
}

interface I1
{
    void X1();
}

interface I2 : I1
{
    void X2();
}

[DynamicInterfaceCastableImplementation]
interface I1_Impl : I1
{
    void I1.X1() => Console.WriteLine("X1 called");
}

[DynamicInterfaceCastableImplementation]
interface I2_Impl : I2
{
    void I2.X2() => Console.WriteLine("X2 called");
}

Output:

X1 called
X1 called
X2 called

Internal previews

File Preview link
docs/fundamentals/code-analysis/quality-rules/ca2256.md Learn preview

Build report

Copilot AI balanced review requested due to automatic review settings October 18, 2026 15:18
@teo-tsirpanis
teo-tsirpanis requested review from a team and gewarren as code owners October 18, 2026 15:18
@dotnetrepoman dotnetrepoman Bot added this to the October 2026 milestone Oct 18, 2026
@dotnet-policy-service dotnet-policy-service Bot added dotnet-fundamentals/svc community-contribution Indicates PR is created by someone from the .NET community. labels Oct 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused documentation change matches the demonstrated behavior and has no unresolved findings.

No open issues 🎉

What changed in this PR

Clarifies when developers can safely suppress CA2256 in the code-analysis documentation.

Changes:

  • Replaces the blanket prohibition with conditional guidance for implementations that support all parent interfaces.
File Description
docs/​fundamentals/​code-analysis/​quality-rules/​ca2256.md Documents the safe suppression condition.

Review effort: Balanced

@BillWagner

Copy link
Copy Markdown
Member

@Youssef1313 PTAL.

I can see that the sample code works correctly, but do we want to recommend the less stringent guidance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates PR is created by someone from the .NET community. dotnet-fundamentals/svc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants