将枚举与“现实世界的价值"相关联,反之亦然 [英] associate enumeration to the ‘real world values’ _and vice versa_

查看:87
本文介绍了将枚举与“现实世界的价值"相关联,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我需要将枚举与现实世界的价值"相关联,反之亦然.

目前,我唯一的想法是制作自己的(通用)类,该类使用.net字典",例如键=枚举,值=现实世界的价值",然后在内部使用字典"再次构建反向映射.

``示例'':

Hello all

I need to associate enumeration to the ‘real world values’ _and vice versa_.

At the moment the only idea I have is, to make my own (generic) class which uses .net ‘Dictionary’ e.g. key= enum, value= ‘real world value’ and then build the reverse mapping -using again ''Dictionary’– internally.

''Example'':

Enum          Real world Values’
-------	      ------------------------
enum1	      "x"
enum2         "thesecond"
.	      .	
.             .
enumN        "whatever"


注意:在我的情况下,真实世界的价值"通常是单个字符值,但我想在真实世界的价值"的意义上使其更通用一些,巫婆可以支持任何类型的唯一键"的任何对象(基于一个或多个属性.

有更好的方法吗?

老实说:我的英语很薄弱,无法找到适合Google的关键字.

提前致谢.问候.


N.B: In my case the ‘Real world Values’ are usually single char values, but I want to make it a little bit more general in the sense of ‘Real world Values’ can be any object witch supports any kind of unique ‘key’ (based on one or multiply properties.

Is there a better way?

To be honest: My English is to weak to find the right keywords for google.

Thanks in advance. Regards.

推荐答案

我在CodeProject文章中介绍了完整的解决方案:
人类可读的枚举元数据 [ ^ ].

该解决方案基于.NET属性.您可以为每个枚举成员具有单独的属性,每个属性都为其赋予一个人类可读的名称,这是微不足道的,因为此解决方案未全球化,因此无法进行本地化.一个更高级的解决方案是使用属性重定向到程序集中嵌入的资源,这种方法不使用代码中的任何字符串常量(这对于支持非常重要),并且首先进行全球化,即可以进行本地化.我在文章中提供了两种方法.

另请参见其他方法"一节中引用的其他作品以及对我之前有关枚举问题的文章的引用.



我不认为您的英语在这里有问题.在发表本文之前,我还进行了一些搜索,并找到了一些令我不满意的解决方案,这就是我开发我的原因的原因.
—SA
I have a comprehensive solution explained in my CodeProject article:
Human-readable Enumeration Meta-data[^].

The solution is based on .NET attributes. You can have separate attributes for each enumeration member each giving it a human readable name, which is trivial, because this solution is not globalized, so it cannot be localized. A more advanced solution is using the attributes redirecting to the resources embedded in the assembly, something which does not use any sting constants in the code (which is very important for support) and globalized in first place, that is, ready for localization. I provided both approaches in my article.

See also other works referenced in the section "Other Approaches" and reference to my previous article on enumeration problem.



I don''t think your English is problem here. I also have done some search before publishing the article and did find some solutions which did not satisfy me, that''s why I developed mine.

—SA


这篇关于将枚举与“现实世界的价值"相关联,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆