其他分享
首页 > 其他分享> > Enum Description多语言的显示(上)

Enum Description多语言的显示(上)

作者:互联网

1.建立Resouce.resx

 

 

 2.建立Resource.zh.resx

 

 

 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.运行结果如下:

 

标签:return,Description,DescriptionAttribute,Enum,value,public,语言
来源: https://www.cnblogs.com/keeplearningandsharing/p/14668130.html