Wednesday, June 8, 2016

That type which Enum.GetValues makes, whatever it is, may just be cast back to your enum's type!

<select id="Locale">
   <option selected></option>
   @foreach (var enumValue in Enum.GetValues(typeof(Language)))
   {
      Language language = (Language)enumValue;
      FieldInfo fieldInfo = language.GetType().GetField(language.ToString());
      object[] attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
      var friendlyName = ((DescriptionAttribute)attrs[0]).Description;
      <option value="@enumValue">@friendlyName</option>
   }
</select>

No comments:

Post a Comment