【问题标题】:how to get the integer value of an enum, when i only have the type of the enum当我只有枚举的类型时,如何获取枚举的整数值
【发布时间】:2011-06-15 06:51:16
【问题描述】:

我认为这个问题需要一些代码:

private TypeValues GetEnumValues(Type enumType, string description)
        {
            TypeValues wtv = new TypeValues();
            wtv.TypeValueDescription = description;
            List<string> values = Enum.GetNames(enumType).ToList();
            foreach (string v in values)
            {
                //how to get the integer value of the enum value 'v' ?????
                wtv.TypeValues.Add(new TypeValue() { Code = v, Description = v });
            }
            return wtv;

        }

这样称呼它:

GetEnumValues(typeof(AanhefType), "some name");

在GetEnumValues 函数中,我有枚举的值。所以我迭代了这些值,我也想得到那个枚举值的整数值。

所以我的值是“红色”和“绿色”,我也想得到 0 和 1。

当我的函数中有枚举时,我可以从字符串创建枚举值并将其转换为该枚举,然后将其转换为 int,但在这种情况下,我没有枚举本身,但是只有枚举的类型。

我也尝试将实际枚举作为参数传递,但我不允许将枚举作为参数传递。

所以现在我被困住了.....

【问题讨论】:

    标签: c# enums


    【解决方案1】:
    private TypeValues GetEnumValues(Type enumType, string description)
            {
                TypeValues wtv = new TypeValues();
                wtv.TypeValueDescription = description;
                List<string> values = Enum.GetNames(enumType).ToList();
                foreach (string v in values)
                {
                    //how to get the integer value of the enum value 'v' ?????
    
                   int value = (int)Enum.Parse(enumType, v);
    
                    wtv.TypeValues.Add(new TypeValue() { Code = v, Description = v });
                }
                return wtv;
    
            }
    

    http://msdn.microsoft.com/en-us/library/essfb559.aspx

    Enum.Parse 将采用 Type 和 String 并返回对枚举值之一的引用 - 然后可以简单地将其强制转换为 int。

    【讨论】:

    • 这行得通!奇怪的是,在即时窗口中尝试了一些代码,还有这个,它没有向我显示 int 值: (int)Enum.Parse(enumType, v) 向我显示 'De Heer' 这是字符串的值'五'。但是在运行代码时,它确实显示了一个 int。奇怪....
    【解决方案2】:

    试试

    (int)Enum.Parse(enumType, v)
    

    【讨论】:

    • +1 如果已经有方法可以执行此操作,则无需自己循环遍历这些值。
    【解决方案3】:
    public static class StringEnum
    {
    
    
        public static string GetStringValue(Enum value)
        {
            string output = null;
            Type type = value.GetType();
    
            FieldInfo fi = type.GetField(value.ToString());
            StringValue[] attr = fi.GetCustomAttributes(typeof(StringValue), false) as StringValue[];
    
            if (attr.Length > 0)
            {
                output = attr[0].Value;
            }
            return output;
        }
    }
    

    是获取字符串值的方法。

    public enum CampaignRequestType { [StringValue("None")] None = 0, [StringValue("Pharmacy Cards")] Pharmacy_Cards = 1,[StringValue("Prospect Campaign")] Prospect_Campaign = 2,[StringValue("Tradeshow/Advertising")] Tradeshow_Advertising = 3 }
    

    它是一个枚举...

    string item = StringEnum.GetStringValue((Enumeration.CampaignRequestType)updateRequestStatus.RequestType_Code);
    

    这里(Enumeration.CampaignRequestType) 是我的枚举 updateRequestStatus.RequestType_Code 是数据库字段 int 类型

    我将 int 值转换为 枚举 类型

    【讨论】:

    • 欢迎来到 StackOverflow !尝试更多地使用代码编辑工具,以便您的答案更具可读性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    相关资源
    最近更新 更多