首页 > 其他 > 详细

C# Enum转换

时间:2014-08-21 14:46:04      阅读:390      评论:0      收藏:0      [点我收藏+]
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转换

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!