如何为枚举提供用户友好的名称?
我有一个像这样的枚举
Enum Complexity
{
NotSoComplex,
LittleComplex,
Complex,
VeryComplex
}
,我想在下拉列表中使用它,但不想在列表中看到这样的骆驼名称(对于用户来说看起来真的很奇怪)。相反,我想用正常的措辞,比如 没那么复杂 有点复杂(等等)
另外,我的应用程序是多语言的,我希望能够显示这些本地化的字符串,我使用一个帮助器 TranslationHelper(string strID) ,它为我提供了字符串 id 的本地化版本。
我有一个可行的解决方案,但不是很优雅: 我为枚举创建一个辅助类,其中一个成员 Complexity 和 ToString() 被覆盖,如下所示(代码简化)
public class ComplexityHelper
{
public ComplexityHelper(Complexity c, string desc)
{ m_complex = c; m_desc=desc; }
public Complexity Complexity { get { ... } set {...} }
public override ToString() { return m_desc; }
//Then a static field like this
private static List<Complexity> m_cxList = null;
// and method that returns the status lists to bind to DataSource of lists
public static List<ComplexityHelper> GetComplexities()
{
if (m_cxList == null)
{
string[] list = TranslationHelper.GetTranslation("item_Complexities").Split(',');
Array listVal = Enum.GetValues(typeof(Complexities));
if (list.Length != listVal.Length)
throw new Exception("Invalid Complexities translations (item_Complexities)");
m_cxList = new List<Complexity>();
for (int i = 0; i < list.Length; i++)
{
Complexity cx = (ComplexitylistVal.GetValue(i);
ComplexityHelper ch = new ComplexityHelper(cx, list[i]);
m_cxList.Add(ch);
}
}
return m_cxList;
}
}
虽然可行,但我对此不满意,因为我必须为需要使用的各种枚举进行类似的编码在选项列表中。
有人对更简单或更通用的解决方案有建议吗?
谢谢 博格丹
I have an enumeration like
Enum Complexity
{
NotSoComplex,
LittleComplex,
Complex,
VeryComplex
}
And I want to use it in a dropdown list, but don't want to see such Camel names in list (looks really odd for users). Instead I would like to have in normal wording, like
Not so complex
Little complex (etc)
Also, my application is multi-lang and I would like to be able to display those strings localized, and I use a helper, TranslationHelper(string strID) which gives me the localized version for a string id.
I have a working solution, but not very elegant:
I create a helper class for the enum, with one member Complexity and ToString() overwritten, like below (code simplified)
public class ComplexityHelper
{
public ComplexityHelper(Complexity c, string desc)
{ m_complex = c; m_desc=desc; }
public Complexity Complexity { get { ... } set {...} }
public override ToString() { return m_desc; }
//Then a static field like this
private static List<Complexity> m_cxList = null;
// and method that returns the status lists to bind to DataSource of lists
public static List<ComplexityHelper> GetComplexities()
{
if (m_cxList == null)
{
string[] list = TranslationHelper.GetTranslation("item_Complexities").Split(',');
Array listVal = Enum.GetValues(typeof(Complexities));
if (list.Length != listVal.Length)
throw new Exception("Invalid Complexities translations (item_Complexities)");
m_cxList = new List<Complexity>();
for (int i = 0; i < list.Length; i++)
{
Complexity cx = (ComplexitylistVal.GetValue(i);
ComplexityHelper ch = new ComplexityHelper(cx, list[i]);
m_cxList.Add(ch);
}
}
return m_cxList;
}
}
While workable, I'm not happy with it, since I have to code it similarily for various enums I need to use in picklists.
Does anyone have a suggestion for a simpler or more generic solution?
Thanks
Bogdan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基本友好名称
使用描述属性:*
以及一个方便的枚举扩展方法:
像这样使用:(
注意这并不完全适用于位标志...)
用于本地化< /strong>
.NET 中有一个完善的模式,用于处理每个字符串值的多种语言 - 使用 资源文件,并扩展扩展方法以从资源文件中读取:
任何时候我们可以利用现有的 BCL 功能来实现我们想要的,这绝对是第一个探索的途径。这最大限度地降低了复杂性,并使用了许多其他开发人员已经熟悉的模式。
将它们放在一起
为了将其绑定到 DropDownList,我们可能希望跟踪控件中的真实枚举值,并将翻译后的友好名称限制为视觉糖。我们可以通过使用匿名类型和列表上的 DataField 属性来做到这一点:
让我们分解该 DataSource 行:
Enum.GetValues(typeof(MyEnum))
,这为我们提供了一个松散的-值的类型化Array
OfType()
,将数组转换为IEnumerable
Select()
并提供一个 lambda,该 lambda 可以投影一个具有两个字段(Description 和 Value)的新对象。DataTextField 和 DataValueField 属性在数据绑定时进行反射式评估,因此它们将搜索 DataItem 上具有匹配名称的字段。
-请注意,在主文章中,作者编写了自己的
DescriptionAttribute
类,这是不必要的,因为 .NET 标准库中已经存在该类。-Basic Friendly names
Use the Description attribute:*
And a handy extension method for enums:
Used like so:
(Note this doesn't exactly work for bit flags...)
For localization
There is a well-established pattern in .NET for handling multiple languages per string value - use a resource file, and expand the extension method to read from the resource file:
Any time we can leverage existing BCL functionality to achieve what we want, that's definitely the first route to explore. This minimizes complexity and uses patterns already familiar to many other developers.
Putting it all together
To get this to bind to a DropDownList, we probably want to track the real enum values in our control and limit the translated, friendly name to visual sugar. We can do so by using an anonymous type and the DataField properties on the list:
Let's break down that DataSource line:
Enum.GetValues(typeof(MyEnum))
, which gets us a loosely-typedArray
of the valuesOfType<MyEnum>()
which converts the array to anIEnumerable<MyEnum>
Select()
and provide a lambda that projects a new object with two fields, Description and Value.The DataTextField and DataValueField properties are evaluated reflectively at databind-time, so they will search for fields on DataItem with matching names.
-Note in the main article, the author wrote their own
DescriptionAttribute
class which is unnecessary, as one already exists in .NET's standard libraries.-像其他答案一样使用属性是一个好方法,但是如果您只想使用枚举值中的文本,则以下代码将根据值的驼峰式大小写进行分割
: >GetDescriptionOf(Complexity.NotSoComplex) 将返回
Not So Complex
。这可以与任何枚举值一起使用。为了使其更有用,您可以将其设为扩展方法:
您现在可以使用
Complexity.NotSoComplex.ToFriendlyString()
调用它以返回Not So Complex
。编辑:刚刚注意到在你的问题中你提到你需要本地化文本。在这种情况下,我将使用一个属性来包含一个键来查找本地化值,但如果找不到本地化文本,则默认使用友好字符串方法作为最后的手段。您可以这样定义枚举:
您还需要以下代码:
要获取本地化描述,您可以调用:
这有几种后备情况:
LocalizedEnum
属性,它将 如果枚举定义了LocalizedEnum
属性,但未找到本地化文本,则使用该键查找翻译文本,, >LocalizedEnum
属性已定义,出现错误时将使用驼峰式分割方法The use of attributes as in the other answers is a good way to go, but if you just want to use the text from the values of the enum, the following code will split based on the camel-casing of the value:
Calling
GetDescriptionOf(Complexity.NotSoComplex)
will returnNot So Complex
. This can be used with any enum value.To make it more useful, you could make it an extension method:
You cal now call it using
Complexity.NotSoComplex.ToFriendlyString()
to returnNot So Complex
.EDIT: just noticed that in your question you mention that you need to localise the text. In that case, I'd use an attribute to contain a key to look up the localised value, but default to the friendly string method as a last resort if the localised text cannot be found. You would define you enums like this:
You would also need this code:
To get the localised descriptions, you would then call:
This has several fallback cases:
LocalisedEnum
attribute defined, it will use the key to look up the translated textLocalisedEnum
attribute defined but no localised text is found, it defaults to using the camel-case split methodLocalisedEnum
attribute defined, it will use the camel-case split method我使用下面的类
读取一个名为描述的属性
I use the following class
Which reads an attribute called description
谢谢大家的所有回答。
最后,我使用了 Rex M 和 adrianbanks 的组合,并添加了我自己的改进,以简化与 ComboBox 的绑定。
需要进行这些更改是因为,在处理代码时,我意识到有时需要能够从组合中排除一个枚举项。
例如
,有时我希望选项列表显示除 AllComplexities 之外的所有内容(在添加 - 编辑模块中),其他时间显示所有内容(在过滤器中)。
这就是我所做的:
枚举值。最后,如果翻译丢失,我只是根据驼峰命名法分割枚举名称来分隔单词,如 adrianbanks 所示。顺便说一句,TranslationHelper 是 resourceMgr.GetString(...) 的包装器。
完整的代码如下所示。
我创建了一个基于 Enum 的通用帮助器类,它允许将枚举轻松绑定到 DataSource
然后我创建了一个通用静态方法,该方法返回List>,如下所示,
以及一个返回包含所有成员的列表的包装器
现在让我们将所有内容放在一起并绑定到实际组合:
要读取选定的值并使用它,我使用如下代码
Thank you all for all answers.
Finally I used a combination from Rex M and adrianbanks, and added my own improvements, to simplify the binding to ComboBox.
The changes were needed because, while working on the code, I realized sometimes I need to be able to exclude one enumeration item from the combo.
E.g.
So sometimes I want the picklist to show all but AllComplexities (in add - edit modules) and other time to show all (in filters)
Here's what I did:
EnumValue. Finally, if translation is missing I just split enum name based on camelcase to separate words as shown by adrianbanks. BTW, TranslationHelper is a wrapper around resourceMgr.GetString(...)
The full code is shown below
I created a generic helper class based on Enum, which allow to bind the enum easily to DataSource
Then I created a generic static method that returns a List>, as below
and a wrapper to return list with all members
Now let's put all things together and bind to actual combo:
To read selected the value and use it, I use code as below