在枚举例如使用特殊字符%(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.

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