Skip to content

Commit 4b1fdf5

Browse files
authored
Add code example for CA2256 rule (#49105) (#49107)
1 parent a551402 commit 4b1fdf5

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

docs/fundamentals/code-analysis/quality-rules/ca2256.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ helpviewer_keywords:
99
- DynamicInterfaceCastableImplementationAnalyzer
1010
- CA2256
1111
author: Youssef1313
12+
dev_langs:
13+
- CSharp
1214
---
1315
# CA2256: All members declared in parent interfaces must have an implementation in a DynamicInterfaceCastableImplementation-attributed interface
1416

@@ -32,10 +34,15 @@ Types attributed with <xref:System.Runtime.InteropServices.DynamicInterfaceCasta
3234

3335
Implement the missing interface members.
3436

37+
## Example
38+
39+
:::code language="csharp" source="snippets/csharp/all-rules/ca2256.cs" id="snippet1" highlight="30":::
40+
3541
## When to suppress errors
3642

3743
Do not suppress a warning from this rule.
3844

3945
## See also
4046

4147
- [Usage warnings](usage-warnings.md)
48+
- [CA2257: Members defined on an interface with the 'DynamicInterfaceCastableImplementationAttribute' should be 'static'](ca2257.md)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace ca2256
4+
{
5+
//<snippet1>
6+
interface IParent
7+
{
8+
void ParentMethod();
9+
}
10+
11+
// This interface violates the rule.
12+
[DynamicInterfaceCastableImplementation]
13+
interface IBadChild : IParent
14+
{
15+
static void ChildMethod()
16+
{
17+
// ...
18+
}
19+
}
20+
21+
// This interface satisfies the rule.
22+
[DynamicInterfaceCastableImplementation]
23+
interface IGoodChild : IParent
24+
{
25+
static void ChildMethod()
26+
{
27+
// ...
28+
}
29+
30+
void IParent.ParentMethod()
31+
{
32+
// ...
33+
}
34+
}
35+
//</snippet1>
36+
}

0 commit comments

Comments
 (0)