Skip to content

Commit

Permalink
Add doc for CA2260
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n committed Oct 6, 2022
1 parent 728ea03 commit b0fefac
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
65 changes: 65 additions & 0 deletions docs/fundamentals/code-analysis/quality-rules/ca2260.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: "CA2260: Implement Generic Math Interfaces Correctly"
description: "Generic math interfaces require the derived type itself to be used for the self recurring type parameter"
ms.date: 10/06/2022
ms.topic: reference
f1_keywords:
- CA2260
- ImplementGenericMathInterfacesCorrectly
helpviewer_keywords:
- ImplementGenericMathInterfacesCorrectly
- CA2260
author: buyaa-n
---
# CA2260: Providing a 'DynamicInterfaceCastableImplementation' interface in Visual Basic is unsupported

| | Value |
| ----------------------------------- | ------------------------------------ |
| **Rule ID** | CA2260 |
| **Category** | [Usage](usage-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |

## Cause

When not passing the type itself as a type parameter when implementing a generic math interface that requires self recurring type parameter.

## Rule description

Some generic math interfaces introduce static abstract members, the only way to access those static members is through a generic constraint, the generic constraints implements Curiously Recurring Template Pattern (CRTP), therefore require the derived type itself to be used for the self recurring type parameter. If a type implements such interface without passing required type parameter and CA2260 is ignored the code would compile successfully but the static abstract will not me accessible therefore the type will not be usable, compiler would warn with CS0315 on such usage.


## How to fix violations

Pass correct type parameter for self recurring type parameter (TSelf) when implementing those interfaces.

### Example

**Violation**:

```csharp
using System;

// Warns: The 'IParsable<TSelf>' requires the 'TSelf' type parameter to be filled with the derived type 'MyDate'
public readonly struct MyDate : IParsable<DateOnly>
{ ... }
```

**Fix**:

If your array's elements are larger than one byte in size, you can multiply the length of the array by the element size to get the number of bytes.

```csharp
using System;

// No warning
public readonly struct MyDate : IParsable<MyDate>
{ ... }
```

## When to suppress errors

Do not suppress a warning from this rule.

## See also

- [Usage warnings](usage-warnings.md)
1 change: 1 addition & 0 deletions docs/fundamentals/code-analysis/quality-rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ The following table lists code quality analysis rules.
> | [CA2256: All members declared in parent interfaces must have an implementation in a DynamicInterfaceCastableImplementation-attributed interface](ca2256.md) | Types attributed with `DynamicInterfaceCastableImplementationAttribute` act as an interface implementation for a type that implements the `IDynamicInterfaceCastable` type. As a result, it must provide an implementation of all of the members defined in the inherited interfaces, because the type that implements `IDynamicInterfaceCastable` will not provide them otherwise. |
> | [CA2257: Members defined on an interface with 'DynamicInterfaceCastableImplementationAttribute' should be 'static'](ca2257.md) | Since a type that implements `IDynamicInterfaceCastable` may not implement a dynamic interface in metadata, calls to an instance interface member that is not an explicit implementation defined on this type are likely to fail at run time. Mark new interface members `static` to avoid run-time errors. |
> | [CA2258: Providing a 'DynamicInterfaceCastableImplementation' interface in Visual Basic is unsupported](ca2258.md) | Providing a functional `DynamicInterfaceCastableImplementationAttribute`-attributed interface requires the Default Interface Members feature, which is unsupported in Visual Basic. |
> | [CA2260: Implement Generic Math Interfaces Correctly](ca2260.md) | Generic math interfaces require the derived type itself to be used for the self recurring type parameter |
> | [CA2300: Do not use insecure deserializer BinaryFormatter](ca2300.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
> | [CA2301: Do not call BinaryFormatter.Deserialize without first setting BinaryFormatter.Binder](ca2301.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
> | [CA2302: Ensure BinaryFormatter.Binder is set before calling BinaryFormatter.Deserialize](ca2302.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ Usage rules support proper usage of .NET.
|[CA2256: All members declared in parent interfaces must have an implementation in a DynamicInterfaceCastableImplementation-attributed interface](ca2256.md) | Types attributed with `DynamicInterfaceCastableImplementationAttribute` act as an interface implementation for a type that implements the `IDynamicInterfaceCastable` type. As a result, it must provide an implementation of all of the members defined in the inherited interfaces, because the type that implements `IDynamicInterfaceCastable` will not provide them otherwise. |
|[CA2257: Members defined on an interface with 'DynamicInterfaceCastableImplementationAttribute' should be 'static'](ca2257.md) | Since a type that implements `IDynamicInterfaceCastable` may not implement a dynamic interface in metadata, calls to an instance interface member that is not an explicit implementation defined on this type are likely to fail at run time. Mark new interface members `static` to avoid run-time errors. |
|[CA2258: Providing a 'DynamicInterfaceCastableImplementation' interface in Visual Basic is unsupported](ca2258.md) | Providing a functional `DynamicInterfaceCastableImplementationAttribute`-attributed interface requires the Default Interface Members feature, which is unsupported in Visual Basic. |
|[CA2260: Implement Generic Math Interfaces Correctly](ca2260.md) | Generic math interfaces require the derived type itself to be used for the self recurring type parameter |
2 changes: 2 additions & 0 deletions docs/fundamentals/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,8 @@ items:
href: code-analysis/quality-rules/ca2257.md
- name: CA2258
href: code-analysis/quality-rules/ca2258.md
- name: CA2260
href: code-analysis/quality-rules/ca2260.md
- name: Code style rules
items:
- name: Overview
Expand Down

0 comments on commit b0fefac

Please sign in to comment.