键值对的枚举键值

2023-09-03 16:51:11 作者:校长.借根烟

我能有一个枚举它作为一个键值对。

Can I have an enum which acts as a key value pair.

public enum infringementCategory
{ 
    Infringement,
    OFN
}

如果我选择侵权我应该得到INF0001如果我选择 OFN 我应该得到INF0002

If I select Infringement I should get "INF0001" and if I select OFN I should get "INF0002"

这可能吗?

推荐答案

您可以以字符串重新presentation与您的枚举值相关联的使用装饰。检查这个问题:枚举的ToString

You can use decorators in order to associate string representation with your enum values. Check this question: Enum ToString

它看起来像:

public enum infringementCategory
{ 
    [Description("INF0001")]
    Infringement,
    [Description("INF0002")]
    OFN
}

看起来更整洁比使用字典和需要较少的维护。

Looks more neat than using Dictionary and needs less maintain.