ENUM協助類

來源:互聯網
上載者:User

 

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();
}
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.