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.")
文字列から
intから
更新:
番号からは、以下のことができます。
キャストするだけです。
範囲内かどうかは、Enum.IsDefinedで確認できます。
次の例を見てみましょう:
。