zoukankan      html  css  js  c++  java
  • 枚举数据源化

    注意:其中可能使用部分自定义的扩展方法用于类型转换,在使用中需要你自己改一下

        /// <summary>
        /// 枚举管理
        /// </summary>
        public class EnumHandler
        {
            /// <summary>
            /// 获取数据源,需再次转换为DataTable
            /// </summary>
            /// <param name="t"></param>
            /// <returns></returns>
            public static IList<ItemInfo> GetList(Type t)
            {
                if (typeof(Enum) != t.BaseType)
                {
                    throw new Exception(t.Name + "类型参数不合法,当前泛型类的类型参数必须为枚举类型");
                }
                IList<ItemInfo> list = new List<ItemInfo>();
                IList<string> listName = Enum.GetNames(t);
                Array listValue = Enum.GetValues(t);
                int i = 0;
                foreach (int item in listValue)
                {
                    ItemInfo info = new ItemInfo(item.ToString(), listName[i]);
                    list.Add(info);
                    i++;
                }
    
                return list;
            }
    
            /// <summary>
            /// 直接获取枚举类型的数据源 DataTable
            /// </summary>
            /// <param name="t"></param>
            /// <returns></returns>
            public static DataTable GetDataTable(Type t)
            {
                return GetList(t).ToDataTable();
            }
        }
    View Code


    以上使用了自定义的类

        /// <summary>
        /// 自定义辅助元素,供类型转换使用
        /// </summary>
        public class ItemInfo
        {
            public string id;
            public string name = string.Empty;
    
            public ItemInfo() { }
            public ItemInfo(string pId, string pName)
            {
                this.id = pId;
                this.name = pName;
            }
    
            public override string ToString()
            {
                return this.name;
            }
    
            //public static ItemInfo Create(string pName)
            //{
            //    return new ItemInfo(pName, pName);
            //}
    
            public static ItemInfo Create(string pId, string pName)
            {
                return new ItemInfo(pId, pName);
            }
    
        }
    View Code

  • 相关阅读:
    软件版本 —— Alpha、Beta、RC版本的区别
    linux删除回收站提示权限错误
    Centos7系统备份和还原脚本
    搭建Docker私服
    在深度Deepin 15.11系统上安装Python 3.7.4版本的方法
    Dockerfile范例
    Remmina无法远程连接的解决方法
    再生龙教程——对于Centos7进行备份和还原
    共享Excel编辑的一些资源
    【转载】怎样编写概要设计
  • 原文地址:https://www.cnblogs.com/bingle/p/3379005.html
Copyright © 2011-2022 走看看