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

Double generic types and members with non-generic equivalents

Aperta
#2,757 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
38/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
csharp
Ambito
cli

Direzione di ricerca

Inizia esaminando l’API generica esistente ParseResult.GetValue e i costruttori di Option, usando come ambito gli overload basati su Type proposti. Confronta il pattern con l’API DI non generica, quindi verifica che le alternative supportino il caso d’uso AOT senza reflection mostrato, preservando al contempo il comportamento generico.

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

Descrizione

Examples

// In addition to these:
T? ParseResult.GetValue<T>(Option<T> option);
new Option<T>(string name, params string[] aliases);
// etc...

// Make it possible to call them like this:
object? ParseResult.GetValue(Option option, Type valueType);
new Option(Type valueType, string name, params string[] aliases);

This is somewhat similar to what we have in DI:

// We can add service like this:
services.AddSignleton<T>();

// Or this:
services.AddSingleton(Type serviceType);

Justification

I am making my own library that adds proper hosting API, making using System.CommandLine similar to any regular ASP.NET project.
For example:

Program.cs

app.MapCommand("foo", ([Option] FileInfo file) =>
{
    foreach (string line in File.ReadLines(file.FullName))
        Console.WriteLine(line);
});

ShellApplication.cs

public void MapCommand(string name, Delegate command)
{
	RootCommand rootCommand = [];
	ParameterInfo[] parameters = command.Method.GetParameters();

	foreach (ParameterInfo parameterInfo in parameters)
	{
		if (parameterInfo.GetCustomAttribute<OptionAttribute>() is not null)
		{
			Option option = (typeof(Option<>).MakeGenericType(parameterInfo.ParameterType).GetConstructor([typeof(string), typeof(string[])])!.Invoke([name]) as Option)!;
			rootCommand.Options.Add(option);
		}
	}
	// etc...
}

At first glance this looks fine – just make a generic type using reflection. Only the problem is that this is not AOT compatible and will cause exceptions. Adding alternatives that accept types as parameters would solve this.

Additional context

I've glossed over the codebase, and it doesn't look like it will be that hard to implement changes. If that's ok with you, I can prepare a PR myself.

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.