在枚举例如使用特殊字符%(C#3.0) [英] Using special character in Enum e.g. % (C#3.0)

查看:162
本文介绍了在枚举例如使用特殊字符%(C#3.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我ahve一个组合,其来源是一个枚举。现在,其他值之间(比如值1,值

I ahve a combo whose source is an Enum. Now , among the other values(say value1, value2

等)有一个项目变动(%)将在组合中显示。

etc.) there is one item Changes(%) that will be displayed in the combo .

如何在枚举定义变动(%)?

How to define Changes(%) in the enum?

使用C#3.0

感谢

推荐答案

您不能。枚举值的名称必须是有效的C#标识符。你不应该试图把显示名称在那里。

You can't. Enum value names have to be valid C# identifiers. You shouldn't be trying to put display names in there.

相反,考虑装饰用 [说明] 属性的每个值,你可以使用反射获取:

Instead, consider decorating each value with a [Description] attribute which you can fetch with reflection:

public enum ChangeType
{
    [Description("Changes (%)")]
    PercentageChanges,

    [Description("Changes (absolute)")]
    AbsoluteChanges
}

另外定义的资源,可能使用枚举值的名称作为资源键。这将是更好地为国际化的目的,二话不说,虽然越来越多的工作,如果你不需要你的应用程序进行国际化。

Alternatively define resources, possibly using the enum value name as the resource key. This will be better for i18n purposes, apart from anything else, although more work if you don't need your app to be internationalized.

编辑:这里的一篇文章进入详细

这篇关于在枚举例如使用特殊字符%(C#3.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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