<分区>
我有一个枚举,用于查找协调字符串值。其中一个枚举中有一个空格,因此我试图使用 description 属性来查找该值。找到 DescriptionAttribute 后,我无法转换回公共(public)类。
public class Address
{
...blah...more class datatypes here...
public AddressType Type { get; set; }
...blah....
public enum AddressType
{
FRA = 0,
JAP = 1,
MEX = 2,
CAN = 3,
[Description("United States")]
UnitedStates = 4,
}
if (Address.Type.ToString() == "UnitedStates")
{
Adddress.Type = GetDescription(Address.Type);
}
private static AddressType GetDescription(AddressType addrType)
{
FieldInfo fi = addrType.GetType().GetField(addrType.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : addrType.ToString();
}
在 GetDescription 方法中,如何将其转换回其公共(public)类数据类型“AddressType”,但失败了,因为这里是一个字符串?