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检查它是否在范围内。
以下面的例子为例: