Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6726607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:56:49+00:00 2026-05-26T09:56:49+00:00

I got the following model piece of code: public enum EnumTest { [Description (Enum

  • 0

I got the following model piece of code:

public enum EnumTest
{
  [Description ("Enum Text 1")]
  Value_1 = 1,

  [Description ("Enum Text 2")]
  Value_2 = 2,
}

public List<Fields> listFields = new List<Fields>();

public class Fields
{
  public int Code { get; set;}
  public string Description { get; set;}
}

I got an Enum and I would like to fill my variable CODE with enum value and the variable Description with the same enum description. I looked up a long time and failed to initialize my “ListFields” into its constructor with the enum VALUE/DESCRIPTION.

I already got the enum and the method to get its description.. I found it usefull, so I’ll leave it here, maybe it can be useful for someone..

 public static string GetDescription(this Enum value)
 {
     return (from m in value.GetType().GetMember(value.ToString())
             let attr =(DescriptionAttribute)m.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault()
             select attr == null ? value.ToString() : attr.Description).FirstOrDefault();
  }

To use this you just need to do something like this:

String xx = Enum.EnumName.GetDescription();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:56:50+00:00Added an answer on May 26, 2026 at 9:56 am

    You have to use reflection.

        public static Fields[] GetEnumFields(Type enumType)
        {
            if (enumType == null)
                throw new ArgumentNullException("enumType");
            if (!enumType.IsEnum)
                throw new ArgumentException("Not an enum");
    
            FieldInfo[] fieldInfos = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
    
            Fields[] result = new Fields[fieldInfos.Length];
            for (int i = 0; i < fieldInfos.Length; ++i)
            {
                FieldInfo field = fieldInfos[i];
    
                int value = (int)field.GetValue(null);
    
                DescriptionAttribute attrib = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                string desc = attrib != null ? attrib.Description : field.Name;
    
                result[i] = new Fields(value, desc);
            }
    
            return result;
        }
    
        public class Fields
        {
            private int value;
            private string description;
    
            public int Value
            {
                get { return this.value; }
            }
    
            public string Description
            {
                get { return this.description; }
            }
    
            public Fields(int value, string description)
            {
                this.value = value;
                this.description = description;
            }
        }
    

    To use it is quite simple:

        enum test
        {
            [Description("hello!")]
            ciao,
    
            www
        }
    
        static void Main(string[] args)
        {
            foreach (Fields f in GetEnumFields(typeof(test)))
            {
                Console.WriteLine(f.Description);
            }
        }
    

    In my implementation when a descriptionattribute is not found, field name is used.

    We must also say that reflection can be slow and rebuilding the entire array when you need it is a waste of time, if you need it often.
    You can store the array somewhere so you can compute it only once and keep it cached.
    This of course and as I said, makes sense only if you need this readonly list very often.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following piece of code on a PHP 5.2.4 (no safe_mode) linux
Hey guys ive got the following code in my Model require 'RMagick' class Upload
I've got the following text box in my Silverlight app: <TextBox Text={Binding Model.PercentOfSale, Mode=TwoWay,
I've got the following model Ext.regModel(Entries, { fields: [ {name: id, type: int}, {name:
I got the following Model: public class ViewBloqueioNotaFiscal { public ViewComboStatus ComboStatus = new
I've got the following model class: public class Product { public int ProductID {get;set;}
I got the following Html: <div class=horizontalRadio> <label for=SearchBag.DisplayTypeChips id=DisplayTypeChipsLabel> <%=ViewData.Model.T9nProvider.TranslateById(CommonWeb.Chips)%> </label> <%=Html.RadioButton(displayType, DisplayTypes.Chip,
I got following code in an XHTML-document and every browser saysit is malformed :(
I've got the following model and I want ShiftRequest and MissionRequest to have a
Hey guys ive got the following Model for Accounts require 'net/http' require 'uri' require

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.