如何将字符串值分配给枚举并在交换机中使用该值

edi*_*ode 22 .net c# enums

基本上,一系列标题将被传递到switch语句中,我需要将它们与枚举的字符串值进行比较.但我几乎不知道如何正确地做到这一点.

另外,我不知道这是否是最好的方法,所以如果有人有任何想法?

例如:

enum
{
    doctor = "doctor",
    mr = "mr",
    mrs = "mrs"
}
Run Code Online (Sandbox Code Playgroud)

然后切换我分配给它们的字符串值.

Mic*_*rls 38

我发现对我来说最好的方法是使用System.ComponentModel.DescriptionAttribute枚举值的属性.

这是一个例子:

using System.ComponentModel;

public enum ActionCode
{
    [Description("E")]
    Edit,
    [Description("D")]
    Delete,
    [Description("R")]
    Restore
}
Run Code Online (Sandbox Code Playgroud)

然后,要使用它,在静态类上创建一个扩展方法,如下所示:

编辑:我重写了方法以包含来自Laurie Dickinson的一个很好的建议,以便该方法在没有Description属性时返回枚举值的名称.我还重构了尝试改进功能的方法.它现在适用于所有枚举而不使用IConvertible.

public static class Extensions
{
    public static string GetDescription(this Enum e)
    {
        var attribute =
            e.GetType()
                .GetTypeInfo()
                .GetMember(e.ToString())
                .FirstOrDefault(member => member.MemberType == MemberTypes.Field)
                .GetCustomAttributes(typeof(DescriptionAttribute), false)
                .SingleOrDefault()
                as DescriptionAttribute;

        return attribute?.Description ?? e.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,要获得与上面的枚举相关联的字符串,我们可以使用以下代码:

using Your.Extension.Method.Namespace;

...

var action = ActionCode.Edit;
var actionDescription = action.GetDescription();

// Value of actionDescription will be "E".
Run Code Online (Sandbox Code Playgroud)

这是另一个示例枚举:

public enum TestEnum
{
    [Description("This is test 1")]
    Test1,
    Test2,
    [Description("This is test 3")]
    Test3

}
Run Code Online (Sandbox Code Playgroud)

以下是查看说明的代码:

var test = TestEnum.Test2;
var testDescription = test.GetDescription();
test = TestEnum.Test3;
var testDescription2 = test.GetDescription();
Run Code Online (Sandbox Code Playgroud)

结果将是:

testDescription: "Test2"
testDescription2: "This is test 3"
Run Code Online (Sandbox Code Playgroud)

我想继续发布泛型方法,因为它更有用.它可以防止您为所有枚举编写自定义扩展.

  • 有效,但速度非常慢 (2认同)

Luk*_*keH 12

你不能拥有enum一个基础类型string.底层类型可以是任何整数类型,除了char.

如果你想将a翻译string成你的enum那么你可能需要使用ParseTryParse方法.

string incoming = "doctor";

// throws an exception if the string can't be parsed as a TestEnum
TestEnum foo = (TestEnum)Enum.Parse(typeof(TestEnum), incoming, true);

// try to parse the string as a TestEnum without throwing an exception
TestEnum bar;
if (Enum.TryParse(incoming, true, out bar))
{
    // success
}
else
{
    // the string isn't an element of TestEnum
}

// ...

enum TestEnum
{
    Doctor, Mr, Mrs
}
Run Code Online (Sandbox Code Playgroud)


Bra*_*ner 5

我相信执行此操作的标准方法是使用具有只读字符串属性的静态类,该属性返回您想要的值。


Mic*_*rls 5

我想为使用 C# 6 或更高版本的任何人添加另一个答案。

如果您只想获取 Enum 值的名称,则可以使用 C# 6 中引入的新 nameof() 方法。

string enumName = nameof(MyEnum.EnumVal1); // enumName will equal "EnumVal1"
Run Code Online (Sandbox Code Playgroud)

虽然这乍一看似乎有点矫枉过正(为什么不直接将字符串的值设置为“EnumVal1”?),但它会给您编译时检查以确保该值有效。因此,如果您更改了枚举值的名称并忘记告诉您的 IDE 查找和替换所有引用,则在您修复它们之前它不会编译。