栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

获取枚举类型的描述description

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取枚举类型的描述description

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace candel
{
    class Program
    {
 static void Main(string args)
 {
     int x = (int)OrderType.Wait/confirm/i;
     string str = Util.GetEnumDesc(typeof(OrderType), x);
     Console.WriteLine(str);

     Order o = new Order() { Id = 1, Type = (int)OrderType.Complete };
     string str2 = (typeof(OrderType)).GetEnumDesc(o.Type);
     Console.WriteLine(str2);

     Console.Read();
 }

    }

    public class Order
    {
 public int Id { set; get; }
 public int Type { set; get; }
    }

    public enum OrderType
    {
 /// 
 /// 等待用户付款
 /// 
[ Description("等待用户付款")]
 WaitPay = 0,
 /// 
 /// 等待商家发货
 /// 
      【  Description("等待商家发货")】
 WaitSend = 1,
 /// 
 /// 等待用户确认收货
 /// 
[ Description("等待用户确认收货")]
 WaitConfirm = 2,
 /// 
 /// 订单完成
 /// 
 [Description("订单完成")]
 Complete = 3
    }

    public static class Util
    {
 /// 
 /// 根据值得到中文备注
 /// 
 /// 
 /// 
 /// 
 public static String GetEnumDesc(this Type e, int? value)
 {
     FieldInfo fields = e.GetFields();
     for (int i = 1, count = fields.Length; i < count; i++)
     {
  if ((int)System.Enum.Parse(e, fieldsi.Name) == value)
  {
      DescriptionAttribute EnumAttributes = (DescriptionAttribute)fieldsi.
  GetCustomAttributes(typeof(DescriptionAttribute), false);
      if (EnumAttributes.Length > 0)
      {
   return EnumAttributes0.Description;
      }
  }
     }
     return "";
 }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/231661.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号