C# 从枚举中获取字符串值
我的枚举定义如下C# 从枚举中获取字符串值,c#,.net,C#,.net,我的枚举定义如下 public enum PatientRaceCodeType { [StringValue("Question not asked")] Q = 8, [StringValue("Unknown")] U = Q | B, [StringValue("Native Hawaiian OR Other Pacific Islander")] P = Q | D, } 我需要获取字符串值,例如,如果U它应该返回“unknown”
public enum PatientRaceCodeType
{
[StringValue("Question not asked")]
Q = 8,
[StringValue("Unknown")]
U = Q | B,
[StringValue("Native Hawaiian OR Other Pacific Islander")]
P = Q | D,
}
我需要获取字符串值,例如,如果U
它应该返回“unknown”
我试过这个:
Enum.GetName(typeof(testRaceCodeType), test.RaceCode)
这是行不通的<代码>测试。Racecode具有值
U
此代码对我有效(testRaceCodeType)Enum.Parse(typeof(testRaceCodeType),test.RaceCode))
.GetStringValue()什么是
B
和D
?看一下答案。我想这就是您想要的。我会使用类型安全枚举,如下面的答案所述: