码迷,mamicode.com
首页 > 其他好文 > 详细

C# Enum转换

时间:2014-08-21 14:46:04      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   ar   div   

public class Enum2
    {
        
        public Dictionary<string, int> GetEnumItems<T>()
        {
            Dictionary<string, int> dicResult = new Dictionary<string, int>();
            T obj = default(T);
            Type type = obj.GetType();
            foreach (string s in type.GetEnumNames())
            {
                dicResult.Add(s, Convert.ToInt16((T)Enum.Parse(typeof(T), s, true)));
            }
            return dicResult;
        }

        public T GetEnumByEnumName<T>(string enumName)
        {
            T result = default(T);
            if (Enum.IsDefined(typeof(T), enumName))
            {
                result = (T)Enum.Parse(typeof(T), enumName, true);
            }
            else
            {

            }
            return result;
        }

        public T GetEnumByEnumIndex<T>(int ienum)
        {
            T result = default(T);
            if (Enum.IsDefined(typeof(T), ienum))
            {
                result = (T)Enum.Parse(typeof(T), ienum.ToString(), true);
            }
            else
            {

            }
            return result;
        }

 

C# Enum转换,布布扣,bubuko.com

C# Enum转换

标签:style   blog   color   os   io   for   ar   div   

原文地址:http://www.cnblogs.com/qxoffice2008/p/3926937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!