<small id='iHZld'></small><noframes id='iHZld'>

      <bdo id='iHZld'></bdo><ul id='iHZld'></ul>
    <i id='iHZld'><tr id='iHZld'><dt id='iHZld'><q id='iHZld'><span id='iHZld'><b id='iHZld'><form id='iHZld'><ins id='iHZld'></ins><ul id='iHZld'></ul><sub id='iHZld'></sub></form><legend id='iHZld'></legend><bdo id='iHZld'><pre id='iHZld'><center id='iHZld'></center></pre></bdo></b><th id='iHZld'></th></span></q></dt></tr></i><div id='iHZld'><tfoot id='iHZld'></tfoot><dl id='iHZld'><fieldset id='iHZld'></fieldset></dl></div>
  1. <legend id='iHZld'><style id='iHZld'><dir id='iHZld'><q id='iHZld'></q></dir></style></legend>

  2. <tfoot id='iHZld'></tfoot>
    1. 从描述属性中获取枚举

      Get Enum from Description attribute(从描述属性中获取枚举)
        <tbody id='LMa6F'></tbody>
        • <i id='LMa6F'><tr id='LMa6F'><dt id='LMa6F'><q id='LMa6F'><span id='LMa6F'><b id='LMa6F'><form id='LMa6F'><ins id='LMa6F'></ins><ul id='LMa6F'></ul><sub id='LMa6F'></sub></form><legend id='LMa6F'></legend><bdo id='LMa6F'><pre id='LMa6F'><center id='LMa6F'></center></pre></bdo></b><th id='LMa6F'></th></span></q></dt></tr></i><div id='LMa6F'><tfoot id='LMa6F'></tfoot><dl id='LMa6F'><fieldset id='LMa6F'></fieldset></dl></div>

          <small id='LMa6F'></small><noframes id='LMa6F'>

          • <bdo id='LMa6F'></bdo><ul id='LMa6F'></ul>
              <tfoot id='LMa6F'></tfoot>

              1. <legend id='LMa6F'><style id='LMa6F'><dir id='LMa6F'><q id='LMa6F'></q></dir></style></legend>
                本文介绍了从描述属性中获取枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                可能重复:
                通过描述属性查找枚举值

                我有一个通用扩展方法,它从 Enum 获取 Description 属性:

                I have a generic extension method which gets the Description attribute from an Enum:

                enum Animal
                {
                    [Description("")]
                    NotSet = 0,
                
                    [Description("Giant Panda")]
                    GiantPanda = 1,
                
                    [Description("Lesser Spotted Anteater")]
                    LesserSpottedAnteater = 2
                }
                
                public static string GetDescription(this Enum value)
                {            
                    FieldInfo field = value.GetType().GetField(value.ToString());
                
                    DescriptionAttribute attribute
                            = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
                                as DescriptionAttribute;
                
                    return attribute == null ? value.ToString() : attribute.Description;
                }
                

                所以我可以...

                string myAnimal = Animal.GiantPanda.GetDescription(); // = "Giant Panda"
                

                现在,我正在尝试在另一个方向上计算出等效的功能,例如...

                now, I'm trying to work out the equivalent function in the other direction, something like...

                Animal a = (Animal)Enum.GetValueFromDescription("Giant Panda", typeof(Animal));
                

                推荐答案

                public static class EnumEx
                {
                    public static T GetValueFromDescription<T>(string description) where T : Enum
                    {
                        foreach(var field in typeof(T).GetFields())
                        {
                            if (Attribute.GetCustomAttribute(field,
                            typeof(DescriptionAttribute)) is DescriptionAttribute attribute)
                            {
                                if (attribute.Description == description)
                                    return (T)field.GetValue(null);
                            }
                            else
                            {
                                if (field.Name == description)
                                    return (T)field.GetValue(null);
                            }
                        }
                
                        throw new ArgumentException("Not found.", nameof(description));
                        // Or return default(T);
                    }
                }
                

                用法:

                var panda = EnumEx.GetValueFromDescription<Animal>("Giant Panda");
                

                这篇关于从描述属性中获取枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                Accessing attribute info from DTE(从 DTE 访问属性信息)
                c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)

                <i id='0aPcg'><tr id='0aPcg'><dt id='0aPcg'><q id='0aPcg'><span id='0aPcg'><b id='0aPcg'><form id='0aPcg'><ins id='0aPcg'></ins><ul id='0aPcg'></ul><sub id='0aPcg'></sub></form><legend id='0aPcg'></legend><bdo id='0aPcg'><pre id='0aPcg'><center id='0aPcg'></center></pre></bdo></b><th id='0aPcg'></th></span></q></dt></tr></i><div id='0aPcg'><tfoot id='0aPcg'></tfoot><dl id='0aPcg'><fieldset id='0aPcg'></fieldset></dl></div>
                    <bdo id='0aPcg'></bdo><ul id='0aPcg'></ul>
                      <tbody id='0aPcg'></tbody>

                      <tfoot id='0aPcg'></tfoot>
                    1. <small id='0aPcg'></small><noframes id='0aPcg'>

                      <legend id='0aPcg'><style id='0aPcg'><dir id='0aPcg'><q id='0aPcg'></q></dir></style></legend>