public static Nullable <TEnum> ParseNullable <TEnum>(Object value) where TEnum : struct
        {
            Nullable <TEnum> result = null;

            if (value == null || value == DBNull.Value)
            {
                result = null;
            }

            else if (value is String)
            {
                result = EnumEx.Parse <TEnum>((String)value);
            }

            else if (value is Char)
            {
                result = EnumEx.Parse <TEnum>((Char)value);
            }

            else if (value is Int32)
            {
                result = EnumEx.Parse <TEnum>((Int32)value);
            }

            return(result);
        }
        public static List <String> GetDescriptions(Type enumType)
        {
            if (!enumType.GetTypeInfo().IsEnum)
            {
                throw new Exception(String.Format("{0} must be an enum type", enumType));
            }

            List <String> result = new List <String>();

            List <Enum> values = EnumEx.ToEnumList(enumType);

            foreach (Enum value in values)
            {
                result.Add(value.ToDescription());
            }

            return(result);
        }
 public static TEnum Parse <TEnum>(Object value) where TEnum : struct
 {
     return(EnumEx.ParseNullable <TEnum>(value).Value);
 }
示例#4
0
        public void ToNameTEnum_WithAttrib_EqualsFirstName()
        {
            var name = EnumEx.ToName <TestEnum>(TestEnum.WithAttrib);

            Assert.AreEqual("With Attrib", name);
        }
示例#5
0
        public void ToName_WithAttrib_EqualsFirstName()
        {
            var name = EnumEx.ToName(typeof(TestEnum), TestEnum.WithAttrib);

            Assert.AreEqual("With Attrib", name);
        }
示例#6
0
        public void ToNameTEnum_OutOfRange_IsNull()
        {
            var name = EnumEx.ToName <TestEnum>((TestEnum)3);

            Assert.AreEqual("3", name);
        }
示例#7
0
        public void ToName_OutOfRange_IsNull()
        {
            var name = EnumEx.ToName(typeof(TestEnum), (TestEnum)3);

            Assert.AreEqual("3", name);
        }