首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Enum数组中获取描述

从Enum数组中获取描述
EN

Stack Overflow用户
提问于 2015-09-18 18:12:35
回答 3查看 1.9K关注 0票数 0

我试图从一个枚举数组中获得描述,这是我目前的结构。

代码语言:javascript
复制
public enum IllinoisNonDisclosureConvictionFormOptions
{
    [Description("625 ILCS 5/11-501 - Driving Under the Influence")]
    answerConviction0 = 0,

    [Description("625 ILCS 5/11-503 - Reckless Driving")]
    answerConviction1 = 1,

    [Description("a violation of Article 11 of the Criminal Code of 1961, not including prostitution under 720 ILCS 5 / 11 - 14")]
    answerConviction2 = 2,

    [Description("720 ILCS 5/26-5 - Dog Fighting")]
    answerConviction3 = 3,

    [Description("720 ILCS 5/12-1 - Assault")]
    answerConviction4 = 4,
}

因此,基本上,用户会选择他们所犯的罪行,然后在if语句中将该文本与枚举的描述进行比较。我现在的情况是:

代码语言:javascript
复制
    if (request.Question4 != null)
    {
        var answersForQuestion4 = Enum.GetValues(typeof(IllinoisNonDisclosureConvictionFormOptions)).Cast<IllinoisNonDisclosureConvictionFormOptions>().ToList();
        foreach (Enum answer in answersForQuestion4)
        {
            //I need to compare the description to the selected text
            string description = (enum description value)
            if (request.Question4 == description)
            {return description}
        }

    }

我可能不得不从枚举切换到ControllersConstants,因为我实际上不需要将它们的答案保存在数据库中。如果你对这件事有什么见解,请告诉我。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-09-18 18:41:31

在阅读完帖子之后,非常感谢你的积极反馈,我意识到我的错误在于,答案被设置为ENUM而不是INT。我相信这种代码格式将给我希望的结果:

代码语言:javascript
复制
if (request.Question4 != null)
{
    var answersForQuestion4 = Enum.GetValues(typeof(IllinoisNonDisclosureConvictionFormOptions)).Cast<IllinoisNonDisclosureConvictionFormOptions>().ToList();
    foreach (int answer in answersForQuestion4)
    {
        string descrip = Enum.GetName(typeof(IllinoisNonDisclosureConvictionFormOptions), answer);
        if(descrip == request.Question4)
        {
            documentModel.Sections.Add(new Section(documentModel, new Paragraph(documentModel, descrip)));
        }
    }
}

这篇文章帮了很多忙!Get Enum from Description attribute

票数 0
EN

Stack Overflow用户

发布于 2015-09-18 18:22:44

这个发布的问题(Get Enum from Description attribute)包括获取枚举描述。

如果您需要相反的答案,并且知道响应必须与您的枚举描述相匹配,则可以使用Max的答案进行反向工作以获得枚举。

票数 1
EN

Stack Overflow用户

发布于 2015-09-18 19:07:40

您发布的解决方案似乎是寻找匹配的枚举名称(例如,answerConviction0),而不是问题中描述的匹配描述。

你可以这样做:

代码语言:javascript
复制
string findMe = "625 ILCS 5/11-503 - Reckless Driving";

Type enumType = typeof(IllinoisNonDisclosureConvictionFormOptions);
Type descriptionAttributeType = typeof(DescriptionAttribute);

foreach (string memberName in Enum.GetNames(enumType))
{
    MemberInfo member = enumType.GetMember(memberName).Single();

    string memberDescription = ((DescriptionAttribute)Attribute.GetCustomAttribute(member, descriptionAttributeType)).Description;

    if (findMe.Equals(memberDescription))
    {
        Console.WriteLine("Found it!");
    }
}

请注意,所有这些反射将是缓慢的。也许您会更好地使用字符串数组,而不是带有描述的枚举。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32658507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档