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

      <small id='3lO29'></small><noframes id='3lO29'>

      1. C# - 按属性获取所有枚举值

        C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
            <bdo id='oRL6g'></bdo><ul id='oRL6g'></ul>
            1. <legend id='oRL6g'><style id='oRL6g'><dir id='oRL6g'><q id='oRL6g'></q></dir></style></legend>
            2. <tfoot id='oRL6g'></tfoot>

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

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

                  本文介绍了C# - 按属性获取所有枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have this following enum :

                  public enum KodEnum
                  {
                      [EnumType(EnumType = "Task")]
                      TaskTab,
                      [EnumType(EnumType = "Task")]
                      TaskReason,
                      [EnumType(EnumType = "Action")]
                      ActionTab,
                      [EnumType(EnumType = "Action")]
                      ActionReason
                  }
                  
                  public class EnumTypeAttribute : Attribute
                  {
                      public string EnumType { get; set; }
                  }
                  

                  And I want to get a list of all the enums that have the EnumType of "Task".

                  How could I do that?

                  解决方案

                  Something like this should get you on the way...

                  var enumValues = (from member in typeof(KodEnum).GetFields()
                                    let att = member.GetCustomAttributes(false)
                                                    .OfType<EnumTypeAttribute>()
                                                    .FirstOrDefault()
                                    where att != null && att.EnumType == "Task"
                                    select member.GetValue(null))
                                   .Cast<KodEnum>()
                                   .ToList();
                  

                  If you want the int value, then just cast it:

                  var enumValues = (from member in typeof(KodEnum).GetFields()
                                    let att = member.GetCustomAttributes(false)
                                                    .OfType<EnumTypeAttribute>()
                                                    .FirstOrDefault()
                                    where att != null && att.EnumType == "Task"
                                    select (int)member.GetValue(null))
                                   .ToList();
                  

                  And all-lambda solution:

                          var enumValues = typeof(KodEnum)
                              .GetFields()
                              .Select(x => new 
                                  { 
                                      att = x.GetCustomAttributes(false)
                                               .OfType<EnumTypeAttribute>()
                                               .FirstOrDefault(), 
                                      member = x 
                                  })
                              .Where(x => x.att != null && x.att.EnumType == "Task")
                              .Select(x => (int)x.member.GetValue(null))
                              .ToList();
                  

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

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

                  相关文档推荐

                  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 中的代码生成属性相关联)
                      <bdo id='sjnc6'></bdo><ul id='sjnc6'></ul>

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

                          <legend id='sjnc6'><style id='sjnc6'><dir id='sjnc6'><q id='sjnc6'></q></dir></style></legend>

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

                            <tbody id='sjnc6'></tbody>