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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:30:04+00:00 2026-06-10T13:30:04+00:00

At start, we have this basic enum. public enum E_Levels { [ValueOfEnum(Low level)] LOW,

  • 0

At start, we have this basic enum.

public enum E_Levels {

    [ValueOfEnum("Low level")]
    LOW,

    [ValueOfEnum("Normal level")]
    NORMAL,

    [ValueOfEnum("High level")]
    HIGH
}

And I would like to get a List<string> whatever the enum. Something like Extensions.GetValuesOfEnum<E_Levels>() which could return a List<string> with “Low level”, “Normal level” and “High level” in it.

StackOF helped me to get one value attribute :

public static class Extensions {

    public static string ToValueOfEnum(this Enum value) {

        FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
        ValueOfEnum[] attribs = fieldInfo.GetCustomAttributes(typeof(ValueOfEnum), false) as ValueOfEnum[];
        return attribs.Length > 0 ? attribs[0].value : null;
    }
}

And I can call this method whatever the enum : E_Levels.LOW.ToValueOfEnum().

Furthermore, StackOF helped me to get a List<string> for a specific enum.
I made this method in a controller :

private List<string> GetLevels() {

List<string> levelsToReturn = new List<string>();
var levels = Enum.GetValues(typeof(E_Levels)).Cast<E_Levels>();
foreach(E_Levels l in levels) 
    levelsToReturn.Add(l.ToValueOfEnum());

return levelsToReturn;
}

But this way requires me to rewrite the same method for each enum.
So I tried to add this generic method my class Extensions :

public static class Extensions {

    public static string ToValueOfEnum(this Enum value) {...}

    public static List<string> GetValuesOf<T>() {

        List<string> levelsToReturn = new List<string>();
        var levels = Enum.GetValues(typeof(T)).Cast<T>();
        foreach(T l in levels) 
        levelsToReturn.Add(l.ToValueOfEnum());

        return levelsToReturn;
    }
}

But in my foreach, .ToValueOfEnum() is an unknown method.

So I am in trouble, I hoped I could find a way to not rewrite again and again the same method for each enum…

  • 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-06-10T13:30:05+00:00Added an answer on June 10, 2026 at 1:30 pm

    Let’s try to keep this more general purpose.

    I have an extension method that could grab attributes off of enum values. This would give you quick access to the attributes.

    public static class EnumExtensions
    {
        public static TAttribute GetAttribute<TAttribute>(this Enum value)
            where TAttribute : Attribute
        {
            var type = value.GetType();
            var name = Enum.GetName(type, value);
            return type.GetField(name)
                .GetCustomAttributes(false)
                .OfType<TAttribute>()
                .SingleOrDefault();
        }
    }
    

    Using this, you could create some queries to get what you want.

    var valuesOfLevels =
        Enum.GetValues(typeof(E_Levels)).Cast<E_Levels>()
            .Select(level => level.GetAttribute<ValueOfEnumAttribute>().Value);
    

    So your GetValuesOf() method (which is not a great name for such a specialty method IMHO) can be written like this:

    public static List<string> GetValuesOf<TEnum>()
        where TEnum : struct // can't constrain to enums so closest thing
    {
        return Enum.GetValues(typeof(TEnum)).Cast<Enum>()
                   .Select(val => val.GetAttribute<ValueOfEnumAttribute>().Value)
                   .ToList();
    }
    

    Now you may call the method like so:

    var levelValues = GetValueOf<E_Levels>();
    // levelValues = { "Low level", "Normal level", "High level" }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this situation where i have to start an activity from my mainActivity.
I have this snippet of shell script: am start -n com.android.gallery3d/com.android.gallery3d.app.MovieActivity -d /sdcard/movie.mp4 sleep
I have this simple code in a WPF application: ThreadStart start = delegate() {
I have this mail script I have to run a few times. To start
I have this form right here, where the user will add the start date
I have this.setVolumeControlStream(AudioManager.STREAM_MUSIC); at the start of all activities in my application so when
So I have this process called svbc_gm.exe, and I start it as the user
Since a few days I'm unable to start resque I have tried debugging this
I have this UITableView app, a basic diary app, in this method XCode says
I know questions like this have been asked numerous times, but not quite this

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.