我有一个包含10个常量的枚举。我还有一个类,其属性定义如下:MyEnumType tyleType;
现在我想检查tyleType
是否不是!=
)让我们说出枚举的最后5个成员。我不想写:
if(tyleType!=MyNumType.10th && tyleType! MyEnumType.9th && /* etc */)
在这种情况下我该怎么办?
答案 0 :(得分:1)
您可以将它们添加到集合中并使用Enumerable.Contains
,例如:
MyNumType[] notAllowed = { MyNumType.Tenth, MyNumType.Ninth, ... };
if(!notAllowed.Contains(tyleType))
{
}
或存储所有允许的白名单,使其更具可读性:
if(allowed.Contains(tyleType)){..}
答案 1 :(得分:0)
您可以使用IsDefined来检查值是否在枚举器中
if(MyNumType.IsDefined(TypeOf(MyNumType), tyleType)
{
//Code
}