Enum Description多语言的显示(上)

1.建立Resouce.resx

Enum Description多语言的显示(上)

 

 

 2.建立Resource.zh.resx

Enum Description多语言的显示(上)

 

 

 3.定义Enum类

public enum EnumDef
    {
        [Description("周一")]
        Monday,
        [Description("周二")]
        TuesDay,
        [Description("周三")]
        WendesDay,
        [Description("周四")]       
        ThursDay,
        [MyLocalizedEnum("MyFriday", NameResourceType=typeof(Resource))]
        Friday
    }

 

 4.定义一个类MyLocalizedEnumAttribute继承DescriptionAttribute

public class MyLocalizedEnumAttribute: DescriptionAttribute
    {
        private PropertyInfo _nameProperty;
        private Type _resourceType;
        public MyLocalizedEnumAttribute(string displayNameKey)
            : base(displayNameKey)
        {

        }

        public Type NameResourceType
        {
            get
            {
                return _resourceType;
            }
            set
            {
                _resourceType = value;
                _nameProperty = _resourceType.GetProperty(this.Description, BindingFlags.Static | BindingFlags.Public);
            }
        }

        public override string Description
        {
            get
            {
                //check if nameProperty is null and return original display name value
                if (_nameProperty == null)
                {
                    return base.Description;
                }
                return (string)_nameProperty.GetValue(_nameProperty.DeclaringType, null);
            }
        }
    }

5.Controller写法如下:

namespace MultipleLanguage.Controllers
{
    public class ShowEnumDescriptionDemoController : Controller
    {
        // GET: ShowEnumDescriptionDemo
        public ActionResult Index()
        {
            Enum monday = MultipleLanguage.Models.EnumDef.Monday;
            ViewBag.Des = GetEnumDescription(monday);
            ViewBag.Des1 = GetEnumDesciptionCLD(monday);
            Enum friday = MultipleLanguage.Models.EnumDef.Friday;
            ViewBag.Des2 = GetEnumDescription(friday);
            return View();
        }

        //法一:MemberInfo.GetCustomAttributes
        private string GetEnumDescription(Enum value)
        {
            // Get the Description attribute value for the enum value
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }

        //法二:Attribute.GetCustomAttributes Method
        private string GetEnumDesciptionCLD(Enum value)
        {
            FieldInfo field = value.GetType().GetField(value.ToString());
            DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
            return attribute == null ? string.Empty : attribute.Description;
        }
    }
}

 

 6.View写法如下:

@{
    ViewBag.Title = "Index";
}

<h2>显示Enum类型的Description值</h2>
<p><h4> 法一:MemberInfo.GetCustomAttributes Method</h4></p>
@ViewBag.Des
<p><h4>法二:Attribute.GetCustomAttributes Method</h4></p>
@ViewBag.Des1
<p><h4>通过Resource获取Description的值</h4></p>
@ViewBag.Des2 

7.运行结果如下:

Enum Description多语言的显示(上)

 

上一篇:在C语言中使用bool


下一篇:mysql优化 个人笔记 非礼勿扰 -m02