• <small id='JnxHJ'></small><noframes id='JnxHJ'>

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

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

        从枚举属性中获取枚举

        Get enum from enum attribute(从枚举属性中获取枚举)
      3. <legend id='tLfUU'><style id='tLfUU'><dir id='tLfUU'><q id='tLfUU'></q></dir></style></legend>

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

              <tbody id='tLfUU'></tbody>

          1. <tfoot id='tLfUU'></tfoot>

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

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

                  问题描述

                  我有

                  public enum Als 
                  {
                      [StringValue("Beantwoord")] Beantwoord = 0,
                      [StringValue("Niet beantwoord")] NietBeantwoord = 1,
                      [StringValue("Geselecteerd")] Geselecteerd = 2,
                      [StringValue("Niet geselecteerd")] NietGeselecteerd = 3,
                  }
                  

                  public class StringValueAttribute : Attribute
                  {
                      private string _value;
                  
                      public StringValueAttribute(string value)
                      {
                          _value = value;
                      }
                  
                      public string Value
                      {
                          get { return _value; }
                      }
                  }
                  

                  我想将我选择的组合框项目中的值放入一个 int 中:

                  And I would like to put the value from the item I selected of a combobox into a int:

                  int i = (int)(Als)Enum.Parse(typeof(Als), (string)cboAls.SelectedValue); //<- WRONG
                  

                  这可能吗?如果可以,怎么做?(StringValue 匹配从组合框中选择的值).

                  Is this possible, and if so, how? (the StringValue matches the value selected from the combobox).

                  推荐答案

                  这里有一个帮助方法,可以为你指明正确的方向.

                  Here's a helper method that should point you in the right direction.

                  protected Als GetEnumByStringValueAttribute(string value)
                  {
                      Type enumType = typeof(Als);
                      foreach (Enum val in Enum.GetValues(enumType))
                      {
                          FieldInfo fi = enumType.GetField(val.ToString());
                          StringValueAttribute[] attributes = (StringValueAttribute[])fi.GetCustomAttributes(
                              typeof(StringValueAttribute), false);
                          StringValueAttribute attr = attributes[0];
                          if (attr.Value == value)
                          {
                              return (Als)val;
                          }
                      }
                      throw new ArgumentException("The value '" + value + "' is not supported.");
                  }
                  

                  要调用它,只需执行以下操作:

                  And to call it, just do the following:

                  Als result = this.GetEnumByStringValueAttribute<Als>(ComboBox.SelectedValue);
                  

                  这可能不是最好的解决方案,因为它与 Als 相关联,您可能希望使此代码可重用.您可能希望从我的解决方案中删除代码以返回属性值,然后像您在问题中所做的那样使用 Enum.Parse.

                  This probably isn't the best solution though as it's tied to Als and you'll probably want to make this code re-usable. What you'll probably want to strip out the code from my solution to return you the attribute value and then just use Enum.Parse as you are doing in your question.

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

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

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  • <bdo id='LpJzs'></bdo><ul id='LpJzs'></ul>
                      <tbody id='LpJzs'></tbody>

                    • <legend id='LpJzs'><style id='LpJzs'><dir id='LpJzs'><q id='LpJzs'></q></dir></style></legend>

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

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