• C# 读取枚举描述信息实例


    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization.Json;
    using System.IO;
    using System.Data;
    using System.ComponentModel;
    using System.Web.UI.WebControls;

    namespace SieCom.YQ.Common
    {
        public class CommondLibray
        {
            public CommondLibray()
            {
                //TODO
            }

            #region 根据枚举值获取枚举描述信息(单个)
            /// <summary>
            /// 根据枚举值获取枚举描述信息(单个)
            /// </summary>
            /// <typeparam name="TEnum">枚举</typeparam>
            /// <param name="value">枚举值</param>
            /// <returns></returns>
            public static string GetEnumDescription<TEnum>(object value)
            {
                string result = string.Empty;
                Type enumType = typeof(TEnum);
                if (enumType.IsEnum)
                {
                    var name = System.Enum.GetName(enumType, Convert.ToInt32(value));
                    if (name != null)
                    {
                        object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (objs != null && objs.Length > 0)
                        {
                            DescriptionAttribute attr = objs[0] as DescriptionAttribute;
                            result = attr.Description;
                        }
                        else
                        {
                            //TODO
                        }
                    }
                    else
                    {
                        //TODO
                    }
                }
                return result;
            }
            #endregion

            #region 获取枚举列表
            /// <summary>
            /// 获取枚举列表
            /// </summary>
            /// <param name="enumType">枚举类型</param>
            /// <param name="IsAll">是否在枚举列表前加入全部</param>
            /// <returns>枚举对应的ListItem</returns>
            public static List<ListItem> GetEnumList(Type enumType, bool IsAll)
            {
                List<ListItem> list = new List<ListItem>();
                if (enumType.IsEnum)
                {
                    if (IsAll)
                        list.Add(new ListItem("--请选择--", "-1"));
                    Type type = typeof(DescriptionAttribute);
                    System.Reflection.FieldInfo[] files = enumType.GetFields();
                    string strText = string.Empty;
                    string strValue = string.Empty;
                    foreach (System.Reflection.FieldInfo field in files)
                    {
                        if (field.IsSpecialName) continue;
                        strValue = field.GetRawConstantValue().ToString();
                        object[] arr = field.GetCustomAttributes(type, true);
                        if (arr.Length > 0)
                            strText = (arr[0] as DescriptionAttribute).Description;
                        else strText = field.Name;

                        list.Add(new ListItem(strText, strValue));
                    }
                }
                return list;
            }
            #endregion
        }
    }

  • 相关阅读:
    Search Insert Position(二分查找)
    c++基础题
    Divide Two Integers(模拟计算机除法)
    Swap Nodes in Pairs(链表操作)
    Letter Combinations of a Phone Number(带for循环的DFS,组合问题,递归总结)
    进程和程序的区别
    Add Two Numbers(链表)
    Longest Substring Without Repeating Characters
    02.友盟项目--原始日志数据生成
    01.友盟项目--nginx服务器配置
  • 原文地址:https://www.cnblogs.com/houzuofeng/p/3253183.html
Copyright © 2020-2023  润新知