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 249999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:28:27+00:00 2026-05-11T21:28:27+00:00

I have an enum representing all material assembly codes in the system: public enum

  • 0

I have an enum representing all material assembly codes in the system:

public enum EAssemblyUnit
{
    [Description("UCAL1")]
    eUCAL1,
    [Description("UCAL1-3CP")]
    eUCAL13CP,
    [Description("UCAL40-3CP")]
    eUCAL403CP, // ...
}

In legacy code in another part of the system, I have objects labeled with strings that match the enum descriptions. Given one of those strings, what’s the cleanest way to get the enum value? I envision something like:

public EAssemblyUnit FromDescription(string AU)
{
    EAssemblyUnit eAU = <value we find with description matching AU>
    return eAU;
}
  • 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-11T21:28:28+00:00Added an answer on May 11, 2026 at 9:28 pm

    You’d need to iterate through all the static fields of the enum (that’s how they’re stored in reflection) finding the Description attribute for each one… when you spot the right one, get the value of the field.

    For example:

    public static T GetValue<T>(string description)
    {
        foreach (var field in typeof(T).GetFields())
        {
            var descriptions = (DescriptionAttribute[]) 
                   field.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (descriptions.Any(x => x.Description == description))
            {
                return (T) field.GetValue(null);
            }
        }
        throw new SomeException("Description not found");
    }
    

    (This is generic just to make it reusable for different enums.)

    Obviously you’d want to cache the descriptions if you’re doing this even slightly frequently, e.g.:

    public static class DescriptionDictionary<T> where T : struct
    {
        private static readonly Dictionary<string, T> Map = 
            new Dictionary<string, T>();
    
        static DescriptionDictionary()
        {
            if (typeof(T).BaseType != typeof(Enum))
            {
                throw new ArgumentException("Must only use with enums");
            }
            // Could do this with a LINQ query, admittedly...
            foreach (var field in typeof(T).GetFields
                     (BindingFlags.Public | BindingFlags.Static))
            {
                T value = (T) field.GetValue(null);
                foreach (var description in (DescriptionAttribute[]) 
                   field.GetCustomAttributes(typeof(DescriptionAttribute), false))
                {
                    // TODO: Decide what to do if a description comes up
                    // more than once
                    Map[description.Description] = value;
                }
            }
        }
    
        public static T GetValue(string description)
        {
            T ret;
            if (Map.TryGetValue(description, out ret))
            {
                return ret;
            }
            throw new WhateverException("Description not found");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 117k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Actually it's not, it's a common approach - I've seen… May 11, 2026 at 10:49 pm
  • Editorial Team
    Editorial Team added an answer That's an interesting idea, I'm not sure if it could… May 11, 2026 at 10:49 pm
  • Editorial Team
    Editorial Team added an answer USPS provides its own API as long as everything is… May 11, 2026 at 10:49 pm

Related Questions

If I bind a WinForms ComboBox to an enum type's values, i.e. combo1.DropDownStyle =
It is often useful to have a field in a DAO whose value comes
I have an enum that looks as follows: public enum TransactionStatus { Open =
I have an enum construct like this: public enum EnumDisplayStatus { None = 1,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.