c# - 如何确定是否为变量分配了枚举中存在的值?
问题描述
我有这个枚举:
namespace J.Enums
{
public enum TH
{
Light = 0,
Dark = 1
}
public static partial class Extensions
{
public static string Text(this TH theme)
{
switch (theme)
{
default:
case TH.Light: return "Light";
case TH.Dark: return "Dark";
}
}
public static TH ToTheme(this string theme)
{
switch (theme)
{
default:
case "Light": return TH.Light;
case "Dark": return TH.Dark;
}
}
}
}
如果我有a
如下变量:
var a = 88;
如何确定 a 的值是否是 Enum 的有效值?在这种情况下,它不会。
解决方案
var isDefined = Enum.IsDefined(typeof(TH), a);
推荐阅读
- snowflake-cloud-data-platform - okta 推动的雪花角色的成员资格限制?
- javascript - 反应可拖动元素,没有滞后
- javascript - 如何将 e 值的小数缩短为仅 2 位
- spring - 使用 SpyBean 推迟 PostConstruct
- javascript - 我如何从第一个 url 获取值并将该值传递给第二个 url 参数?
- assembly - 如何正确操作装配中的堆栈?
- python - 编写 DataFrames 的函数可防止 workbook.close()
- python - AttributeError:“str”对象没有属性“text”错误
- ios - 是什么导致此 BadDeviceToken 响应?
- c++ - 使用抽象基类为模板参数类型强制使用公共接口是不好的做法吗?