ENUM帮助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;

namespace CommonFunction
{
   public class EnumClass
    {
       private static object refLock = new object();
       private static EnumClass instance=null;

       public static EnumClass Instance
       {
           get {
               lock (refLock)
               {
                   if (instance == null)
                   {
                       lock (refLock)
                       {
                           instance = new EnumClass();
                       }
                   }
               }
               return instance;
           }
       }

      
       /// <summary>
       /// 获取指定枚举类型的键值对集合
       /// </summary>
       /// <param name="type">枚举类型</param>
       /// <returns></returns>
       public Hashtable GetKeysAndValues(Type type)
       {
           if (!CheckType(type))
           {
               return null;
           }

           Hashtable list = new Hashtable();
           foreach (string key in Enum.GetNames(type))
           {
               string val=Enum.Format( type, Enum.Parse(type, key), "d");
               list.Add(key,val);
           }

           return list;
       }


       /// <summary>
       /// 获取枚举类型元素集合
       /// </summary>
       /// <param name="type">枚举类型</param>
       /// <returns></returns>
       public Array GetKeys(Type type)
       {
           if (!CheckType(type))
           {
               return null;
           }

           //检索指定枚举中常数值的数组
           return Enum.GetValues(type);
       }


       /// <summary>
       /// 检验枚举类型
       /// </summary>
       /// <param name="type">类型</param>
       /// <returns></returns>
       private bool CheckType(Type type)
       {
           if (type == null)
           {
               return false;
           }

           if (!type.IsEnum)
           {
               throw new Exception(type.FullName + "不是枚举类型");
           }

           return true;
       }


       /// <summary>
       /// 获取枚举类型值集合
       /// </summary>
       /// <param name="type">枚举类型</param>
       /// <returns></returns>
       public List<int> GetValues(Type type)
       {
           Array array = GetKeys(type);
           List<int> list = new List<int>();
           foreach (object obj in array)
           {
               list.Add((int)obj);
           }

           return list;
       }
       
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace CommonFunction
{
    class Program
    {
        enum Days { Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6 };
        static void Main(string[] args)
        {
            Hashtable hashtable= EnumClass.Instance.GetKeysAndValues(typeof(Days));
            Console.WriteLine("---------枚举类型键值对---------");
            foreach (DictionaryEntry de in hashtable)
            {
                Console.WriteLine(de.Key+" = "+de.Value);
            }
            Console.WriteLine();

            Console.WriteLine("---------枚举类元素---------");
            Array array = EnumClass.Instance.GetKeys(typeof(Days));
            foreach (Days day in array)
            {
                Console.WriteLine(day);
            }
            Console.WriteLine();

            Console.WriteLine("---------枚举类值---------");
            List<int> list = EnumClass.Instance.GetValues(typeof(Days));
            foreach (int i in list)
            {
                Console.WriteLine(i);
            }
            Console.ReadLine();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值