I have an enum which I use to find a coordinating string value. One of these enums has a space in it which I'm thus trying to use the description attribute to find that value. I'm having trouble casting back to the public class after it finds the DescriptionAttribute.
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();
}
Within the GetDescription method how do I cast it back to its public class data type 'AddressType' it fails because here its a string?