Enum Description多语言的显示(上)
2021/4/16 18:25:53
本文主要是介绍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多语言的显示(上)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升
- 2024-05-08代码报错不用愁,CodeGeeX一键完成代码修复、错误解释的功能上线了!