If your question is similar to "I have an enum MyEnum { OneEnumMember, OtherEnumMember } type, enum MyEnum { OneEnumMember, OtherEnumMember } , and I would like to have a function that reports whether this enumeration type contains a member with a specific name, what you are looking for is the System.Enum.IsDefined method System.Enum.IsDefined :
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 type enum that has the Flags attribute, and I would like to have a function that tells if this instance contains a specific enumeration value, then the function looks like something like:
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 can help.
florien Mar 27 '17 at 18:32 2017-03-27 18:32
source share