简体   繁体   English

从枚举数组获取描述

[英]Getting Description from an array of Enums

I am trying to get the description from an array of enums bascically this is the current structure that I have. 我试图从枚举数组中获得基本的描述,这是我目前的结构。

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,
}

So basically a user will select the crime they committed and then that text will be compared in a if statement to the description of the enums. 因此,基本上,用户将选择他们所犯罪行,然后将该文本在if语句中与枚举的描述进行比较。 What I currently have is this: 我目前所拥有的是:

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

    }

I may have to switch from enums to ControllersConstants since I dont really need to save their answers in the database. 由于我真的不需要将他们的答案保存在数据库中,因此我可能必须从枚举切换到ControllersConstants。 Please let me know if you have any insights regarding this matter. 如果您对此事有任何见解,请告诉我。

This posted question ( Get Enum from Description attribute ) includes getting the enum description. 此发布的问题( 从Description属性获取枚举 )包括获取枚举描述。

If you need the opposite and know that the responses MUST match your enum description, you can work backwards to get the enum using Max's answer. 如果您需要相反的说明,并且知道响应必须与您的枚举描述相匹配,则可以使用Max的答案进行反向操作以获取枚举。

The solution you posted seems to be looking for a matching enum name (eg answerConviction0) instead of a matching description as described in the question. 您发布的解决方案似乎正在寻找匹配的枚举名称(例如answerConviction0),而不是问题中描述的匹配描述。

You can do something like this: 您可以执行以下操作:

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!");
    }
}

Note that all this reflection will be slow. 请注意,所有这些反射将很慢。 Maybe you would be better off with an array of strings instead of an enumeration with descriptions. 也许最好使用字符串数组而不是带有描述的枚举。

Thank you so much for the positive feedback after reading through the posts I realized that my mistake was that answer was being set to an ENUM instead of an INT. 非常感谢您在阅读完帖子后的积极反馈,我意识到我的错误是答案被设置为ENUM而不是INT。 I believe that this code format will give me the desired result: 我相信这种代码格式将给我理想的结果:

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

This post helped a lot! 这篇文章很有帮助! Get Enum from Description attribute 从描述属性获取枚举

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM