Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

`Command` class violates design rule CA1010

Aperta
#2,751 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
csharp
Ambito
cli

Direzione di ricerca

Inizia dalla dichiarazione collegata in src/System.CommandLine/Command.cs e riproduci CA1010 con AnalysisMode impostato su Recommended o All, usando l'esempio MyCommand. Il lavoro è completato quando i comandi che ereditano pubblicamente non emettono più CA1010, preservando al contempo il comportamento esistente dei figli di Command.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

The System.CommandLine.Command class implements non-generic IEnumerable, but it does not implement IEnumerable<T>.

https://github.com/dotnet/command-line-api/blob/cf5fd8d696450a48d3cc75a7a1792d34b5303f88/src/System.CommandLine/Command.cs#L25

This causes classes inheriting from the Command class to violate design rule CA1010.
For projects with AnalysisMode set to Recommended or All, warning CA1010 will occur.

Build succeeded.

/home/smdn/temp/cli/Lib.cs(5,14): warning CA1010: Type 'MyCommand' directly or indirectly inherits 'IEnumerable' without implementing 'IEnumerable<T>'. Publicly-visible types should implement the generic version to broaden usability. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1010) [/home/smdn/temp/cli/cli.csproj]
    1 Warning(s)
    0 Error(s)

This simply triggers a warning and does not corrupt any functionality of System.CommandLine.
However, since the cause of the warning is not easy to find out and it may cause confusion, so I think this should be fixed.

Steps to reproduce

Set AnalysisMode to Recommended or All in the csproj file.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <!-- Enable design rules including CA1010 -->
    <AnalysisMode>Recommended</AnalysisMode>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.CommandLine" Version="2.0.1" />
  </ItemGroup>

</Project>

Create a class that inherits from the Command class.

using System.CommandLine;

namespace MyLibrary;

// warning CA1010: Type 'MyCommand' directly or indirectly inherits 'IEnumerable' without implementing 'IEnumerable<T>'. Publicly-visible types should implement the generic version to broaden usability. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1010)
public class MyCommand(string name, string? description) : Command(name, description) { }

Workarounds

The following workarounds are available to avoid this warning.

Workaround 1: disable warning CA1010

#pragma warning disable CA1010
public class MyCommand(string name, string? description) : Command(name, description)
{
}
#pragma warning restore CA1010

Workaround 2: implement IEnumerator<Symbol>

public class MyCommand(string name, string? description) : Command(name, description)
{
    // Implement IEnumerator<Symbol> in order to suppress warning CA1010.
    public IEnumerator<Symbol> GetEnumerator() => Children.GetEnumerator();
}

Workaround 3: reduce AnalysisMode

    <!-- Reduce `AnalysisMode` to either `None`, `Default`, or `Minimum`. -->
    <AnalysisMode>Minimum</AnalysisMode>
Lingua principale
C#
Stelle
3.7k
Fork
434
Merge medio
4h 46m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/command-line-api

Tutte le issue di dotnet/command-line-api

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.