Comment convertir un int en enum en C# ?
Comment convertir un int en enum en C# ?
Comment convertir un int en enum en C# ?
Re: Comment convertir un int en enum en C# ?
À partir d'un int :
```
YourEnum foo = (YourEnum)yourInt;
```
À partir d'une chaîne :
```
YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);
// La vérification foo.ToString().Contains(",") est nécessaire pour
// les énumérations marquées avec l'attribut [Flags].
if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(","))
{
throw new InvalidOperationException(
$"{yourString} is not an underlying value of the YourEnum enumeration."
);
}
```
Dyn
```
YourEnum foo = (YourEnum)yourInt;
```
À partir d'une chaîne :
```
YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);
// La vérification foo.ToString().Contains(",") est nécessaire pour
// les énumérations marquées avec l'attribut [Flags].
if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(","))
{
throw new InvalidOperationException(
$"{yourString} is not an underlying value of the YourEnum enumeration."
);
}
```
Dyn