检查int是否在Enum中. [英] Check if int is in Enum.

查看:82
本文介绍了检查int是否在Enum中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有flags属性的枚举.如何检查int是否在枚举中.如果检查例如枚举中的两个ored值,则Enum.IsDefiend返回false.
在此先谢谢您.

I have an enum with flags attribute. How to check if int is in enum. Enum.IsDefiend returns false if check for example two ored values from enum.
Thanks in advance.

推荐答案

这是这样做的方法:

This is how to do it:

static bool IsIntInEnum(System.Type enumType, int value) {
   if (enumType == null) return false;
   if (!enumType.IsEnum) return false;
   var values = System.Enum.GetValues(enumType);
   if (values == null) return false;
   if (values.Length < 1) return false;
   foreach (object enumvalue in values)
       if (enumvalue is int) break; else return false;
   System.Predicate<int> predicate = (item) => { return (item & value) > 0; };
   return System.Array.Exists<int>((int[])values, predicate);
}



重要的是要了解属性System.FlagsAttribute仅影响通过ToStringstring.Format的值的字符串表示形式.同样,无论是否具有此属性,枚举成员都不必是2的幂.当枚举成员集表示位集值与一组连续值的混合时,存在相当实际的应用程序(例如,在Windows API中).

—SA



It is important to understand that the attribute System.FlagsAttribute only affects the string presentation of the value via ToString or string.Format. Also, with or without this attribute, enumeration members does not have to be powers of two. There are quite practical applications (in Windows API, for example) when the set of enumeration members represents the mixture of bit set values with, say, a set of consecutive values.

—SA


我知道您已经接受了答案,但是这是一种面向数学的方式(也有更多代码):

I know you''ve already accepted an answer, but here''s a more math-oriented way (more code, too):

bool IsFlagsValid(Type t, int value)
{
    bool valid = false;
    if (t.IsEnum)
    {
        valid = true;
        int maxBit = Convert.ToInt32(Math.Pow(2, Math.Ceiling(Math.Log(value)/Math.Log(2)))) >> 2;
        int i = 1;
        do
        {
            if (0 != (value & (1 << i)))
            {
                valid = (Enum.IsDefined(t, 1 << i));
                if (!valid)
                {
                    break;
                }
            }
            i++;
        } while (maxBit > i);
    }
    return valid;
}



这样称呼:



Call it like this:

if (IsFlagsValid(MyFlagsEnum, 25))
{
    // do something if the value was valid
} 


此处是显示如何执行您所要求的内容的文章: [ ^ ].不幸的是,这不是一个容易的修复.

另一个稍微有点怪异的选择是从您的枚举中获取.ToString()的结果并调用Int32.TryParse.如果结果为true,则表示该值以整数字符串形式返回,而不是命名的枚举或枚举的组合.

Here''s an article showing how to do what you are asking: ''Extending'' the Enum Class to Validate the Enum Values with the Flags Attribute[^]. Unfortunately, this is not an easy fix.

Another slightly hacky alternative would be to take the result of .ToString() from your enum and call Int32.TryParse. If the result is true, that means the value is returning as an integer string, not a named enum or combination of enums.

//Example
int myThrowAwayInt;
MyEnum MyEnumValue = (MyEnum)2|8;
bool notDefined = Int32.TryParse(MyEnumValue.ToString(), out myThrowAwayInt);
if(notDefined)
{
    //This enum value is not defined
}


这篇关于检查int是否在Enum中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆