EnumMember
属性のvalue
引数がシリアル化用にあります。目的を表示しない。 MSDN Documentationを参照してください。
この値を取得するには、シリアル化してXMLを解析する必要があります。
もう一つの方法は、あなた自身のヘルパーメソッドを作成し、C#、内蔵DescriptionAttribute
の利点を取ることです。
public enum PaymentCurrency
{
[DescriptionAttribute("CA$")]
CAD,
[DescriptionAttribute("US$")]
USD,
EURO
}
次に、あなたがこれを行うことができますEnumUtils
クラスで独自のヘルパーメソッド使用:
public class EnumUtils
{
public static string stringValueOf(Enum value)
{
var fi = value.GetType().GetField(value.ToString());
var attributes = (DescriptionAttribute[]) fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
{
return attributes[0].Description;
}
else
{
return value.ToString();
}
}
public static object enumValueOf(string value, Type enumType)
{
string[] names = Enum.GetNames(enumType);
foreach (string name in names)
{
if (stringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
{
return Enum.Parse(enumType, name);
}
}
throw new ArgumentException("The string is not a description or value of the specified enum.");
}
}
を