Ejemplo n.º 1
0
        /// <summary>
        /// 获取枚举成员的名称
        /// </summary>
        /// <typeparam name="T">枚举名,比如Enum1</typeparam>
        /// <param name="member">枚举成员实例或成员值,
        /// 范例:Enum1枚举有两个成员A=0,B=1,则传入Enum1.A或0,获取成员名称"A"</param>
        public static string GetMemberName <T>(object member)
        {
            //转成基础类型的成员值
            Type   underlyingType = GetUnderlyingType(typeof(T));
            object memberValue    = UtilityConvert.ConvertTo(member, underlyingType);

            //获取枚举成员的名称
            return(Enum.GetName(typeof(T), memberValue));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取枚举成员的值
        /// </summary>
        /// <typeparam name="T">枚举名,比如Enum1</typeparam>
        /// <param name="memberName">枚举成员的常量名,
        /// 范例:Enum1枚举有两个成员A=0,B=1,则传入"A"获取0</param>
        public static object GetMemberValue <T>(string memberName)
        {
            //获取基础类型
            Type underlyingType = GetUnderlyingType(typeof(T));

            //获取枚举实例
            T instance = GetInstance <T>(memberName);

            //获取枚举成员的值
            return(UtilityConvert.ConvertTo(instance, underlyingType));
        }