public static class EnumExtensions
    {
        public static string GetDescription(this Enum value)
        {
            return value.GetType()
                .GetMember(value.ToString())
                .FirstOrDefault()?
                .GetCustomAttribute<DescriptionAttribute>()?
                .Description;
        }
    }
    public enum RoleiDAt
    {
        [Description("商家")]
        Business = 7,
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(RoleiDAt.Business.ToString());                // 输出 键
            Console.WriteLine((int)RoleiDAt.Business);                      //输出 值
            Console.WriteLine((RoleiDAt)Enum.Parse(typeof(RoleiDAt), "7")); //输出  值 的键名
            Console.WriteLine(Enum.IsDefined(typeof(RoleiDAt),7));          //检查 值 是否存在
            Console.WriteLine(Enum.IsDefined(typeof(RoleiDAt), "Customer"));//检查 键 是否存在
            Console.WriteLine(RoleiDAt.Business.GetDescription());          //输出 描述
        (RoleiDAt)Enum.Parse(typeof(RoleiDAt), "dd");
Console.ReadKey(true); } }

 

posted on 2019-04-13 17:34  Aaron_Net  阅读(343)  评论(0编辑  收藏  举报