C# enum contains value

后端 未结 10 653
轮回少年
轮回少年 2020-12-07 23:46

I have an enum

enum myEnum2 { ab, st, top, under, below}

I would like to write a function to test if a given value is included in myEnum

相关标签:
10条回答
  • 2020-12-08 00:39

    Also can use this:

        enum myEnum2 { ab, st, top, under, below }
        static void Main(string[] args)
        {
            myEnum2 r;
            string name = "ab";
            bool result = Enum.TryParse(name, out r);
        }
    

    The result will contain whether the value is contained in enum or not.

    0 讨论(0)
  • 2020-12-08 00:42

    If your question is like "I have an enum type, enum MyEnum { OneEnumMember, OtherEnumMember }, and I'd like to have a function which tells whether this enum type contains a member with a specific name, then what you're looking for is the System.Enum.IsDefined method:

    Enum.IsDefined(typeof(MyEnum), MyEnum.OneEnumMember); //returns true
    Enum.IsDefined(typeof(MyEnum), "OtherEnumMember"); //returns true
    Enum.IsDefined(typeof(MyEnum), "SomethingDifferent"); //returns false
    

    If your question is like "I have an instance of an enum type, which has Flags attribute, and I'd like to have a function which tells whether this instance contains a specific enum value, then the function looks something like this:

    public static bool ContainsValue<TEnum>(this TEnum e, TEnum val) where Enum: struct, IComparable, IFormattable, IConvertible
    {
        if (!e.GetType().IsEnum)
            throw new ArgumentException("The type TEnum must be an enum type.", nameof(TEnum));
    
        dynamic val1 = e, val2 = val;
        return (val1 | val2) == val1;
    }
    

    Hope I could help.

    0 讨论(0)
  • 2020-12-08 00:43
       public static T ConvertToEnum<T>(this string value)
        {
            if (typeof(T).BaseType != typeof(Enum))
            {
                throw new InvalidCastException("The specified object is not an enum.");
            }
            if (Enum.IsDefined(typeof(T), value.ToUpper()) == false)
            {
                throw new InvalidCastException("The parameter value doesn't exist in the specified enum.");
            }
            return (T)Enum.Parse(typeof(T), value.ToUpper());
        }
    
    0 讨论(0)
  • 2020-12-08 00:44

    Why not use

    Enum.IsDefined(typeof(myEnum), value);
    

    BTW it's nice to create generic Enum<T> class, which wraps around calls to Enum (actually I wonder why something like this was not added to Framework 2.0 or later):

    public static class Enum<T>
    {
        public static bool IsDefined(string name)
        {
            return Enum.IsDefined(typeof(T), name);
        }
    
        public static bool IsDefined(T value)
        {
            return Enum.IsDefined(typeof(T), value);
        }
    
        public static IEnumerable<T> GetValues()
        {
            return Enum.GetValues(typeof(T)).Cast<T>();
        }
        // etc
    }
    

    This allows to avoid all this typeof stuff and use strongly-typed values:

    Enum<StringSplitOptions>.IsDefined("None")
    
    0 讨论(0)
提交回复
热议问题