1. 程式人生 > >Enum To List(轉)

Enum To List(轉)

名稱 small sources 代碼 技術分享 src mat expired erb

技術分享
    public enum ResourceState
    {
        /// <summary>
        /// 下架
        /// </summary>
        [Description("下架")]
        SoldOut = 0,
        /// <summary>
        /// 上架
        /// </summary> 
        [Description("上架")]
        Putaway = 1,

        /// <summary>
        /// 交易成功
        
/// </summary> [Description("交易成功")] Success = 2, /// <summary> /// 廢標 /// </summary> [Description("廢標")] AbandonedTender = 6, /// <summary> /// 違約標 /// </summary> [Description("違約標")] DefaultMark
= 7, /// <summary> /// 中標 /// </summary> [SetClassification(Type = 5)] [Description("中標")] WinTheBidding = 3, /// <summary> /// 流標 /// </summary> [SetClassification(Type = 6)] [Description("流標")] FlowStandard
= 4, /// <summary> /// 未中標 /// </summary> [SetClassification(Type = 4)] [Description("未中標")] LoseABid = 5, /// <summary> /// 競價中 /// </summary> [SetClassification(Type = 2)] [Description("競價中")] Bidding = 8, /// <summary> /// 競拍中 /// </summary> [SetClassification(Type = 3)] [Description("競拍中")] Auctioning = 9, /// <summary> /// 已處理(針對於流標資源) /// </summary> [Description("已處理")] Alreadyprocessed = 10, /// <summary> /// 已過期 /// </summary> [Description("已過期")] ExpiredTime = 11, /// <summary> /// 所有報價 /// </summary> [SetClassification(Type = 1)] [Description("所有報價")] All = 12 } /// <summary> /// 添加自定義屬性 /// 作用:過濾枚舉類型 /// </summary> public class SetClassificationAttribute : Attribute { /// <summary> /// 分類 /// </summary> public int Type { get; set; } public SetClassificationAttribute() { } }
View Code

定義一個枚舉,想要顯示 classification type 從1 到 6的數據到一個list,list中對象有描述,值,type等屬性

技術分享
public class EnumberCreditType
    {
        /// <summary>  
        /// 枚舉的描述  
        /// </summary>  
        public string Desction { set; get; }

        /// <summary>  
        /// 枚舉名稱  
        /// </summary>  
        public string Key { set; get; }

        /// <summary>  
        /// 枚舉對象的值  
        /// </summary>  
        public int Value { set; get; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Name { get; set; }


        /// <summary>
        /// 分類
        /// </summary>
        public int Classification { set; get; }
    }
View Code

代碼如下:

技術分享
   public static List<EnumberCreditType> EnumToList<T>()
        {
            List<EnumberCreditType> list = new List<EnumberCreditType>();

            foreach (var e in Enum.GetValues(typeof(T)))
            {
                EnumberCreditType m = new EnumberCreditType();
                object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute da = objArr[0] as DescriptionAttribute;
                    m.Desction = da.Description;
                }
                //SetClassification 
                object[] setClassificationArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(SetClassificationAttribute), true);
                if (setClassificationArr != null && setClassificationArr.Length > 0)
                {
                    SetClassificationAttribute da = setClassificationArr[0] as SetClassificationAttribute;
                    m.Classification = da.Type;
                }
                //Display
                object[] disArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DisplayNameAttribute), true);
                if (disArr != null && disArr.Length > 0)
                {
                    DisplayNameAttribute da = disArr[0] as DisplayNameAttribute;
                    m.Name = da.DisplayName;
                }

                m.Value = Convert.ToInt32(e);
                m.Key = e.ToString();
                list.Add(m);
            }
            return list;
        }
View Code

var query = EnumToList<ResourceState>().Where(e => e.Classification >= 1 && e.Classification <= 6).OrderBy(e => e.Classification).ToList();

Enum To List(轉)