枚举

枚举

最近项目需要我的枚举以中文名显示,不知道该怎么实现,百度了之后发现还是有方法的,于是在这里将它记录下来。
首先先定义一个枚举

enum TypeEnum
{
    Length=0,
    Width,
    Height
}

枚举是值类型的,每个枚举成员均具有相关联的常数值,所以实际上TypeEnum.Length是等于0的,对它进行tostring操作才等于”Length”,那么如何才能让他翻译成中文呢。就对他进行如下操作:

enum TypeEnum
{
    [Description("长度")]
    Length=0,
    [Description("宽度")]
    Width,
    [Description("高度")]
    Height
}

上述的Description实际上是来自于.Net自带的类,他是用来描述特性用的,本例中Description是对枚举的一个解释说明,那么如何让这个解释以字符型表示出来呢。

public class EnumHelper
{
    /// <summary>
    /// 根据枚举对象获得枚举的说明
    /// </summary>
    /// <param name="obj">枚举对象</param>
    /// <returns></returns>
    public static string GetDescriptionFromEnum(Enum obj)
    {
        string objName = obj.ToString();
        Type t = obj.GetType();
        System.Reflection.FieldInfo fi = t.GetField(objName);
        System.ComponentModel.DescriptionAttribute[] arrDesc = (System.ComponentModel.DescriptionAttribute[])fi.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);

        return arrDesc[0].Description;
    }
    /// <summary>
    /// 根据枚举说明获得枚举对象
    /// </summary>
    /// <typeparam name="T">枚举的类型</typeparam>
    /// <param name="a">枚举说明</param>
    /// <returns>枚举对象</returns>
    public static T GetEnumFromDescription<T>(string a)
        where T : struct
    {
        if (!typeof(T).IsEnum)
        {
            throw new Exception();
        }
        foreach (T b in Enum.GetValues(typeof(T)))
        {
            if (GetDescriptionFromEnum(b as Enum) == a)
                return b;
        }
        return default(T);
    }
    /// <summary>
    /// 根据枚举Tostring的值获得枚举对象
    /// </summary>
    /// <typeparam name="T">枚举Enum</typeparam>
    /// <param name="a">枚举Tostring的值</param>
    /// <returns></returns>
    public static T GetEnumFromName<T>(string a)
        where T : struct
    {
        if (!typeof(T).IsEnum)
        {
            throw new Exception();
        }
        return (T)Enum.Parse(typeof(T), a);
    }
    /// <summary>
    /// 根据枚举Tostring的值获得枚举的说明
    /// </summary>
    /// <typeparam name="T">枚举Enum</typeparam>
    /// <param name="a">枚举Tostring的值</param>
    /// <returns></returns>
    public static string GetDescriptionFromName<T>(string a)
        where T : struct
    {
        if (!typeof(T).IsEnum)
        {
            throw new Exception();
        }
        T type = (T)Enum.Parse(typeof(T), a);
        string objName = type.ToString();
        Type ty = typeof(T);
        System.Reflection.FieldInfo fi = ty.GetField(objName);
        System.ComponentModel.DescriptionAttribute[] arrDesc = (System.ComponentModel.DescriptionAttribute[])fi.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);

        return arrDesc[0].Description;
    }
}

上述的GetDescription方法是对枚举进行操作,返回该枚举的Description。
有时候又需要当只有中文名的时候,获取相应的枚举。那么上述的GetEnum方法就能将传进来的中文名转换为具体枚举,其中T是泛型,指枚举类型,本例中特指TypeEnum。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值