zoukankan      html  css  js  c++  java
  • c# EnumUtil

    public class EnumUtil
        {
            /// <summary>       /// convert obj to Enum
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static T Obj2Enum<T>(object obj)
            {
                if (obj != null)
                {
                    if (!string.IsNullOrEmpty(obj.ToString().Trim()))
                    {
                        try
                        {
                            T t = (T)Enum.Parse(typeof(T), obj.ToString().Trim());
                            return t;
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                return default(T);
            }
            /// <summary>      /// Convert Enum to DataTable 
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public static DataTable Enum2Dt<T>()
            {
                Type t = typeof(T);
                DataTable dt = new DataTable();
                dt.Columns.Add("EnumValue");
                dt.Columns.Add("EnumText");
                foreach (int obj in Enum.GetValues(t))
                {
                    DataRow dr = dt.NewRow();
                    dr["EnumValue"] = obj.ToString();
                    dr["EnumText"] = Enum.GetName(t, obj);
                    dt.Rows.Add(dr);
                }
                return dt;
            }
            /// <summary>     /// get enum value
            /// </summary>
            /// <param name="v"></param>
            /// <returns></returns>
            public static int GetEnumValue(Enum v)
            {
                return v.GetHashCode();          
            }
    
            public static string GetEnumString<T>(object obj)
            {
                if (obj==null)
                {
                    return null;
                }
    
                T t = (T)Enum.Parse(typeof(T), obj.ToString().Trim());
                return t.ToString();
            }
    
            /// <summary>
            /// zlh 20111119
            /// get enum name
            /// </summary>
            /// <param name="v"></param>
            /// <returns></returns>
            public static string GetEnumString(Enum v)
            {
                return v.ToString();          
            }
            /// <summary>        /// convert enum to list
            /// </summary>
            /// <param name="type"></param>
            /// <returns></returns>
            public static IList<EnumEntity> Enum2List<T>()
            {
                
                IList<EnumEntity> list = new List<EnumEntity>();
                foreach (int obj in Enum.GetValues(typeof(T)))
                {
                    EnumEntity item = new EnumEntity();
                    item.EnumValue= obj; ;
                    item.EnumText = Enum.GetName(typeof(T), obj);
                    list.Add(item);
                }
    
                return list;
            }
    
    
    
        }
    
        /// <summary>   /// key value 
        /// </summary>
        public class EnumEntity
        {
            public string EnumText { get; set; }
            public int EnumValue { get; set; }
        }
  • 相关阅读:
    「十二省联考2019」 春节十二响
    「八省联考2018」 劈配
    斯特林数
    「POJ2505」A multiplication game [博弈论]
    [luogu2048] [bzoj2006] [NOI2010] 超级钢琴 题解
    [HNOI2002]-洛谷2234-营业额统计-Treap
    平衡树Treap模板与原理
    KMP算法讲解
    高斯消元--模板,原理
    第一篇博客!!
  • 原文地址:https://www.cnblogs.com/gengaixue/p/3116756.html
Copyright © 2011-2022 走看看