• 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; }
        }
  • 相关阅读:
    主机连接不上虚拟机或虚拟机桥接没有网络
    asp web 报表
    heart or house?
    CPointer
    Raid
    Dos for by 随风
    不支持uri格式
    FabEdge V0.4 新特性:支持多集群通讯
    运维监控
    /etc缩写
  • 原文地址:https://www.cnblogs.com/gengaixue/p/3116756.html
Copyright © 2020-2023  润新知