CommunityToolkit / CommunityToolkit/dotnet

Feature request: Guard.IsDefined<TEnum>()

Aperta
#482 1 commento 12 reazioni 0 assegnatari Vedi su GitHub
feature request :mailbox_with_mail:
Lingua principale
C#
Stelle
3.8k
Fork
400
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

### Overview

I've thoroughly enjoyed the usefulness of the `Guard` API, but one thing it's missing is enum validation (for non-flags enums).

The one issue is that the generic `Enum.IsDefined<>` seems to only be available from .NET 5 onwards, which might be the reason this API hasn't made its way yet to the library, and whether silently boxing the value in pre-NET5 builds is an acceptable thing to do for parameter validation.

### API breakdown

Example:

```cs
public static void IsDefined(TEnum value, [CallerArgumentExpression("value")] string name = "")
where TEnum : struct, Enum
{
if (Enum.IsDefined(value))
return;

ThrowHelper.ThrowArgumentOutOfRangeException(name, value, null); // or a descriptive message
}
```

### Usage example

```cs
public class Something
{
public DayOfWeek Value { get; }

public Something(DayOfWeek value)
{
Guard.IsDefined(value);

Value = value;
}
}
```

### Breaking change?

No

### Alternatives

User can call `IsDefined` and `ThrowArgumentOutOfRangeException` or `ThrowArgumentException` directly.

### Additional context

Not sure if `IsDefined` or `IsEnumDefined` is a better value.

### Help us help you

Yes, I'd like to be assigned to work on this item

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Parti dal punto di ingresso IsDefined della Guard API e verifica come ThrowHelper gestisce la convalida degli argomenti tra i target .NET supportati. Conferma il comportamento generico di convalida degli enum per gli enum non-flag e aggiungi la copertura per i valori definiti e non definiti; il lavoro è completo quando l'uso proposto di Guard.IsDefined funziona senza una breaking change.

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

Valutazione

Stack tecnologico
csharp
Ambito
developer-experience
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.