中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

枚舉(ju)技巧~為枚舉(ju)加Describe屬性,輸出(chu)枚舉(ju)元素的說明信(xin)息

這是枚舉公用屬(shu)性類(lei):

#region 枚舉屬性擴展類

    /// <summary>

    /// 枚舉擴展(zhan)方法

    /// </summary>

    public static class EnumExtensions

    {

        public static string GetDescription(this Enum obj)

        {

            return GetDescription(obj, false);

        }

        public static string GetDescription(this Enum obj, bool isTop)

        {

            if (obj == null)

            {

                return string.Empty;

            }

            try

            {

                Type _enumType = obj.GetType();

                DescriptionAttribute dna = null;

                if (isTop)

                {

                    dna = (DescriptionAttribute)Attribute.GetCustomAttribute(_enumType, typeof(DescriptionAttribute));

                }

                else

                {

                    FieldInfo fi = _enumType.GetField(Enum.GetName(_enumType, obj));

 

                    dna = (DescriptionAttribute)Attribute.GetCustomAttribute(

 

                       fi, typeof(DescriptionAttribute));

                }

                if (dna != null && string.IsNullOrEmpty(dna.Description) == false)

                    return dna.Description;

            }

            catch

            {

                throw;

            }

            return obj.ToString();

 

        }

 

    }

    #endregion

 

一個普通的枚舉

 public enum OpreateType

    {

        [Description("添加")]

        Add = 0,

        Del = 1,

        Update = 2,

        Import = 3,

    }

 

輸(shu)出它指定枚舉元素的(de)描(miao)述(shu)信(xin)息

  Console.WriteLine(OpreateType.Add.GetDescription());

 

posted @ 2011-07-08 14:33  張占嶺  閱讀(1106)  評論(0)    收藏  舉報