从枚举中获取DescriptionAttribute [英] Get DescriptionAttribute from enum

查看:110
本文介绍了从枚举中获取DescriptionAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举,我用来找到一个协调的字符串值。其中一个枚举有一个空格,因此我试图使用description属性来找到该值。找到DescriptionAttribute后,我无法将其转换回公共类。

  public class Address 
{
... blah ... more class datatypes here ...

public AddressType Type {get;组; }

... blah ....

}

  public enum AddressType 
{
FRA = 0,
JAP = 1,
MEX = 2,
CAN = 3,
[描述(美国)]
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)?属性[0]。描述:addrType.ToString();
}

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

解决方案

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



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

  //字符串值区分大小写
private AddressType StringToEnum(string enumString)
{
AddressType returnValue ;
switch(enumString)
{
caseUnited States:
returnValue = AddressType.UnitedStates;
break;
caseFrance:
returnValue = AddressType.FRA;
break;
case日本:
returnValue = AddressType.JAP;
break;
caseMexico:
returnValue = AddressType.MEX;
break;
caseCanada:
returnValue = AddressType.CAN;
break;
default:
returnValue = AddressType.UnitedStates;
break;

}
return returnValue;
}

如果您要将字符串转换为您需要做的枚举这样的事情。


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?

解决方案

You wont be able to directly cast a string to an enum. You will need to write a converter method that takes a string and returns the enum.

Simple Example but you could use a dictionary and make it its own class.

//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;
            }

if you are looking to convert a string to an enum you will need to do something like this.

这篇关于从枚举中获取DescriptionAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆