C/C++教程

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

 

这篇关于Enum Description多语言的显示(上)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!