如何在 C# 中检索枚举值作为字符串
How to retrieve an Enums value as a string in C#
我有以下枚举:
public enum DocumentState
{
Default = 0,
Draft = 1,
Archived = 2,
Deleted = 3
}
在我的解决方案中的大多数地方,我都将它用作普通枚举。我在 int 中使用的一些地方是这样的:
(int)DocumentState.Default)
但是,有些地方,例如当我使用 Examine(它只接受字符串,而不是整数作为输入)时,我需要传递 enumns int 值,就好像它是一个字符串一样。这可以通过以下方式完成:
((int)DocumentState.Default).ToString()
我现在的问题是;真的没有其他方法可以将枚举值检索为字符串吗?
我知道我可能在滥用枚举,但有时这是给定情况下的最佳方法。
您可以参考 System.Runtime.Serialization
并使用 EnumMember
属性。
public enum foonum
{
[EnumMember(Value="success")]
success,
[EnumMember(Value="fail")]
fail
}
Console.WriteLine (foonum.success);
产量:"success"
你可以使用Enum.GetName方法
使用DocumentState.Default.ToString("d")
。
参见 https://msdn.microsoft.com/en-us/library/a0h36syw(v=vs.110).aspx
你好像在滥用枚举。如果您需要存储其他信息,请使用真实的 classes。在这种情况下,您的 Document
-class 可能有一个 State
-属性 那个 returns 一个 DocumentState
class 的实例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()
、f.e.:
Document doc = new Document();
doc.State = new DocumentState(DocStateType.Default);
string docTypeId = doc.State.TypeId.ToString();
此方法使用枚举值作为 TypeId
,通常您不会将枚举值用于您的业务逻辑。所以他们应该是独立的。
就我个人而言,我认为您尝试使用 Enum 做的事情并不是最好的方法。您可以做的是创建一个 class,其中有一个 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 } };
}
}
这样您就可以随时调用您的字典键来检索所需的值,而不会错误地覆盖字典的默认值。
我有以下枚举:
public enum DocumentState
{
Default = 0,
Draft = 1,
Archived = 2,
Deleted = 3
}
在我的解决方案中的大多数地方,我都将它用作普通枚举。我在 int 中使用的一些地方是这样的:
(int)DocumentState.Default)
但是,有些地方,例如当我使用 Examine(它只接受字符串,而不是整数作为输入)时,我需要传递 enumns int 值,就好像它是一个字符串一样。这可以通过以下方式完成:
((int)DocumentState.Default).ToString()
我现在的问题是;真的没有其他方法可以将枚举值检索为字符串吗?
我知道我可能在滥用枚举,但有时这是给定情况下的最佳方法。
您可以参考 System.Runtime.Serialization
并使用 EnumMember
属性。
public enum foonum
{
[EnumMember(Value="success")]
success,
[EnumMember(Value="fail")]
fail
}
Console.WriteLine (foonum.success);
产量:"success"
你可以使用Enum.GetName方法
使用DocumentState.Default.ToString("d")
。
参见 https://msdn.microsoft.com/en-us/library/a0h36syw(v=vs.110).aspx
你好像在滥用枚举。如果您需要存储其他信息,请使用真实的 classes。在这种情况下,您的 Document
-class 可能有一个 State
-属性 那个 returns 一个 DocumentState
class 的实例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()
、f.e.:
Document doc = new Document();
doc.State = new DocumentState(DocStateType.Default);
string docTypeId = doc.State.TypeId.ToString();
此方法使用枚举值作为 TypeId
,通常您不会将枚举值用于您的业务逻辑。所以他们应该是独立的。
就我个人而言,我认为您尝试使用 Enum 做的事情并不是最好的方法。您可以做的是创建一个 class,其中有一个 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 } };
}
}
这样您就可以随时调用您的字典键来检索所需的值,而不会错误地覆盖字典的默认值。