Ako možno v jazyku C# premeniť int
na enum
?
Z reťazca:
YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);
// the foo.ToString().Contains(",") check is necessary for enumerations marked with an [Flags] attribute
if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(","))
throw new InvalidOperationException($"{yourString} is not an underlying value of the YourEnum enumeration.")
Z int:
YourEnum foo = (YourEnum)yourInt;
Aktualizácia:
Z čísla môžete tiež
YourEnum foo = (YourEnum)Enum.ToObject(typeof(YourEnum) , yourInt);
Jednoducho ho odlievajte:
MyEnum e = (MyEnum)3;
Môžete skontrolovať, či je v rozsahu pomocou Enum.IsDefined:
if (Enum.IsDefined(typeof(MyEnum), 3)) { ... }
Vezmite si nasledujúci príklad:
int one = 1;
MyEnum e = (MyEnum)one;