如何在C#中将枚举值作为字符串检索

3

我有以下枚举:

public enum DocumentState
{
    Default = 0,
    Draft = 1,
    Archived = 2,
    Deleted = 3
}

在我的解决方案中,大多数情况下我将其用作普通枚举。有些地方我会像这样将其强制转换为int:

(int)DocumentState.Default)

但是,有些情况下,例如当我使用Examine时(它只接受字符串而不是整数作为输入),我需要将枚举的int值传递下去,就像它是一个字符串一样。可以通过以下方式完成:

((int)DocumentState.Default).ToString()

我的问题是:真的没有其他方法可以将枚举值作为字符串检索吗?

我知道我可能在滥用枚举,但有时这是在特定情况下最好的方法。


1
DocumentState.Default.ToString() 返回 "Default" - Squazz
1
其实不是的。你这个/你的方法有什么问题呢?你可以放弃使用枚举,改用 Dictionary<int, string> 来替代。 - Jehof
2
我倾向于认为你在这里滥用了枚举。 - Drew Kennedy
1
为什么你需要使用枚举的int值呢?也许你应该考虑使用一个真正的类,它有一个DocumentState和一个int属性,例如ID。如果你需要将其作为字符串,那么document.Id.ToString()非常易读和可靠。 - Tim Schmelter
@DrewKennedy:添加了一个类的示例。虽然代码更多,但非常易于维护和扩展,如果需要的话。因此比简单的枚举更健壮和可重用。您还可以通过直接在文档类中存储“StateId”来简化它。 - Tim Schmelter
显示剩余6条评论
5个回答

9

3
你是唯一回答这个帖子实际问题的人。说到这里,我不确定 val.ToString("d") 是否比 ((int)val).ToString() 更好。 - LukeH
3
嗯,C语言风格的强制类型转换总是让我不舒服... - Marvin Sielenkemper

0

看起来你滥用了枚举。如果需要存储额外的信息,请使用真正的类。在这种情况下,你的 Document 类可以有一个 State 属性,返回一个 DocumentState 类的实例,该实例具有 DocStateType 枚举的属性。然后,如果需要添加其他信息,例如 TypeId,获取字符串的代码非常简单和可读:

public class Document
{
    public int DocumentId { get; set; }
    public DocumentState State { get; set; }
    // other properties ...
}


public enum DocStateType
{
    Default = 0,
    Draft = 1,
    Archived = 2,
    Deleted = 3
}

public class DocumentState
{
    public DocumentState(DocStateType type)
    {
        this.Type = type;
        this.TypeId = (int) type;
    }
    public DocumentState(int typeId)
    {
        if (Enum.IsDefined(typeof (DocStateType), typeId))
            this.Type = (DocStateType) typeId;
        else
            throw new ArgumentException("Illegal DocStateType-ID: " + typeId, "typeId");
        this.TypeId = typeId;
    }

    public int TypeId { get; set; }
    public DocStateType Type { get; set; }
    // other properties ...
}

如果您想要将 TypeId 作为字符串,则只需使用doc.State.TypeId.ToString(),例如:
Document doc = new Document();
doc.State = new DocumentState(DocStateType.Default);
string docTypeId = doc.State.TypeId.ToString();

这种方法将枚举值用作 TypeId,通常不会将枚举值用于业务逻辑。因此它们应该是独立的。


-1

个人认为,你试图使用枚举来实现的方法并不是最好的。你可以创建一个类,其中包含一个 Dictionary<TKey, TValue> 来保存你的键和值。

在 C# 6 或更高版本中:

static class DocumentState {
    public static Dictionary<string, int> States { get; } = new Dictionary<string, int>() { { "Default", 0 }, { "Draft", 1 }, { "Archived", 2 }, { "Deleted", 3 } };
}

C# 5或更低版本:

class DocumentState {
    public Dictionary<string, int> State { get; }

    public DocumentState() {
        State = new Dictionary<string, int>() { { "Default", 0 }, { "Draft", 1 }, { "Archived", 2 }, { "Deleted", 3 } };
    }
}

通过这种方式,您可以随时调用字典键来检索所需的值,并且不会错误地覆盖字典的默认值。


-2

1
我对枚举的名称或描述不感兴趣,而是更关注其值。 - Squazz

-3
你可以引用 System.Runtime.Serialization 并使用 EnumMember 属性。
public enum foonum
{
    [EnumMember(Value="success")]
    success,
    [EnumMember(Value="fail")]
    fail
}

Console.WriteLine (foonum.success);

返回: "成功"


3
OP只关心枚举类型的整数值,但希望以字符串形式表示。 - Tim Schmelter
我猜我们都没有读问题。 - Crowcoder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接