【问题标题】:Get DescriptionAttribute from enum [duplicate]从枚举中获取 DescriptionAttribute [重复]
【发布时间】:2013-03-06 05:34:19
【问题描述】:

我有一个枚举,用于查找协调字符串值。其中一个枚举中有一个空格,因此我试图使用 description 属性来查找该值。在找到 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();            
    }

在 GetDescription 方法中,我如何将它转换回其公共类数据类型“AddressType”,因为这里它是一个字符串,所以它失败了?

【问题讨论】:

  • 枚举是AddrType,但是你给你的方法传递了AddressType类型的参数。当您的方法的返回类型为 AddressType 时,您还尝试返回 string
  • 很抱歉应该提到枚举是另一个公共类的一部分,见上文。
  • 您仍然会遇到问题(至少考虑到您发布的代码),因为类型名称不匹配(AddressType vs AddrType)和方法的返回类型你提供的不是string
  • 这就是我遇到的问题是字符串。我需要将其转换回 AddressType,addrType 只是我认为的本地名称。
  • 所以你想通过一个AddressType到方法中得到string的描述,然后再转换回原来的AddressType?为什么?

标签: c# enums


【解决方案1】:

恐怕我不能 100% 确定您要的是什么,但以下方法返回 string 描述或提供的 AddressType 的名称。

private static string 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();            
}

注意返回类型string

【讨论】:

  • 是的,我遇到问题的地方是我希望将它作为 AddressTpye 返回,因为它是我的类的一部分,它是一个枚举
【解决方案2】:

您将无法直接将字符串转换为枚举。您将需要编写一个转换器方法,该方法接受一个字符串并返回枚举。

简单示例,但您可以使用字典并使其成为自己的类。

//string values are case sensitive
    private AddressType StringToEnum(string enumString)
            {
                AddressType returnValue;
                switch (enumString)
                {
                    case "United States":
                        returnValue = AddressType.UnitedStates;
                        break;
                    case "France":
                        returnValue = AddressType.FRA;
                        break;
                    case "Japan":
                        returnValue = AddressType.JAP;
                        break;
                    case "Mexico":
                        returnValue = AddressType.MEX;
                        break;
                    case "Canada":
                        returnValue = AddressType.CAN;
                        break;
                    default:
                        returnValue = AddressType.UnitedStates;
                        break;

                }
                return returnValue;
            }

如果您希望将字符串转换为枚举,则需要执行以下操作。

【讨论】:

    【解决方案3】:

    您可以使用辅助方法从字符串中删除空格并找到正确的枚举

    例子:

    public T EnumFromString<T>(string value) where T : struct
    {
        string noSpace = value.Replace(" ", "");
        if (Enum.GetNames(typeof(T)).Any(x => x.ToString().Equals(noSpace)))
        {
            return (T)Enum.Parse(typeof(T), noSpace);
        }
        return default(T);
    }
    

    用法:

        public enum Test
        {
            UnitedStates,
            NewZealand
        }
    
        Test MyEnum = EnumFromString<Test>("New Zealand"); // Returns 'NewZealand'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 2011-05-21
      相关资源
      最近更新 更多