Check that integer type belongs to enum member

前端 未结 7 1299
情歌与酒
情歌与酒 2020-12-30 22:44

I want to check that some integer type belongs to (an) enumeration member.

For Example,

public enum Enum1
{
    member1 = 4,

    member2 = 5,

    m         


        
相关标签:
7条回答
  • 2020-12-30 22:53

    You can use Enum.GetValues to get all defined values. Then check if your value exists in that list.
    http://msdn.microsoft.com/en-us/library/system.enum.getvalues.aspx

    0 讨论(0)
  • 2020-12-30 22:54
    int testNum = 5;
    bool isMember = Enum.GetValues(typeof(Enum1)).Cast<int>().Any(x => x == testNum);
    
    0 讨论(0)
  • 2020-12-30 22:54

    Be careful this won't work if you have an enum for 3 (Apples and Pears) the methods above won't detect it as valid.

    [Flags]
    public enum Fruit
    { 
    
        Apples=1,
    
        Pears=2,
    
        Oranges =4,
    
    }
    
    0 讨论(0)
  • 2020-12-30 23:01

    You look through the values of the enum and compare them to the integer.

        static bool EnumTest(int testVal, Enum e)
        {
            bool result = false;
            foreach (var val in Enum.GetValues(typeof(Enum1)))
            {
                if ((int)val == testVal)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
    

    Edit: Looks like Sam has a better solution.

    0 讨论(0)
  • 2020-12-30 23:05

    Here's a succinct little snippet from an extension method I wrote a few years ago. Combines TryParse with IsDefined to do it all in one swoop and handle values that don't exist in the enum.

    if (value != null)
    {
        TEnum result;
        if (Enum.TryParse(value.ToString(), true, out result))
        {
            // since an out-of-range int can be cast to TEnum, double-check that result is valid
            if (Enum.IsDefined(typeof(TEnum), result.ToString() ?? string.Empty))
            {
                return result;
            }
        }
    }
    

    Here's the extension for integer values

    public static TEnum ParseToEnum<TEnum>(this int value, TEnum? defaultValue = null, bool useEnumDefault = false) where TEnum : struct
    {
        return ParseToEnumInternal(value, defaultValue, useEnumDefault);
    }
    

    And a usage

    public enum Test
    {
        Value1 = 1,
        Value2 = 3
    }
        
    var intValue = 1;
    var enumParsed = intValue.ParseToEnum<Test>(); // converts to Test.Value1
    intValue = 2;
    enumParsed = intValue.ParseToEnum<Test>(); // either throws or converts to supplied default
    enumParsed = 3.ParseToEnum<Test>(); // converts to Test.Value2
    

    Some people don't like how it dangles off the end of the (potentially nullable) value, but I have an extension that handles null values of nullable types (int?) and I like it myself, so ...

    I can post like a Gist of the whole extension method with all the overloads if you're interested.

    0 讨论(0)
  • 2020-12-30 23:09

    Use Enum.IsDefined

    Enum.IsDefined(typeof(Enum1), 4) == true
    

    but

    Enum.IsDefined(typeof(Enum1), 1) == false
    
    0 讨论(0)
提交回复
热议问题