EnumMember
속성의 value
인자가 직렬화를위한 것입니다. 목적을 표시하지 않습니다. MSDN Documentation을 참조하십시오.
이 값을 얻으려면 직렬화 한 다음 XML을 구문 분석해야합니다.
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.");
}
}
: 당신이 할 수있는 그런 다음
EnumUtils
클래스에서 자신의 헬퍼 메소드를 사용하여
public enum PaymentCurrency
{
[DescriptionAttribute("CA$")]
CAD,
[DescriptionAttribute("US$")]
USD,
EURO
}
을 :
또 다른 방법은 자신의 도우미 메서드를 작성하고 DescriptionAttribute
에 내장 된 C#을 활용하는 것입니다