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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:39:23+00:00 2026-06-18T05:39:23+00:00

I created a method which takes an enum and transforms it in a Dictionary

  • 0

I created a method which takes an enum and transforms it in a Dictionary where each int is associated with the name (as string) of the enum

// Define like this
public static Dictionary<int, string> getDictionaryFromEnum<T>()
{
   List<T> commandList = Enum.GetValues(typeof(T)).Cast<T>().ToList();
   Dictionary<int, string> finalList = new Dictionary<int, string>();
   foreach (T command in commandList)
   {
    finalList.Add((int)(object)command, command.ToString());
   }
 return finalList;
 }

(ps. yes, I have a double cast but the application is meant to be a very cheap-and-dirty C#-enum to Javascript-enum converter).

This can be easily used like this

private enum _myEnum1 { one = 1001, two = 1002 };
private enum _myEnum2 { one = 2001, two = 2002 };
// ... 
var a = getDictionaryFromEnum<_myEnum1>();
var b = getDictionaryFromEnum<_myEnum2>();

Now, I was wondering whether I could create a list of enums to use for a series of calls to iterate my calls.

This was in the original question: [Why can’t I call this?]

What should I do to be able to create a call like this one?

List<Type> enumsToConvertList = new List<Type>();
enumsToConvertList.Add(typeof(_myEnum1));
enumsToConvertList.Add(typeof(_myEnum2));
// this'll be a loop
var a = getDictionaryFromEnum<enumsToConvertList.ElementAt(0)>();
  • 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-18T05:39:24+00:00Added an answer on June 18, 2026 at 5:39 am

    You can’t specify generic argument type at runtime (well, without reflection). So, simply create non-generic method, which accepts argument of Type type:

    public static Dictionary<int, string> getDictionaryFromEnum(Type enumType)
    {
        return Enum.GetValues(enumType).Cast<object>()
                   .ToDictionary(x => (int)x, x => x.ToString());
    }
    

    Usage:

    List<Type> enumsToConvertList = new List<Type>();
    enumsToConvertList.Add(typeof(_myEnum1));
    enumsToConvertList.Add(typeof(_myEnum2));
    
    var a = getDictionaryFromEnum(enumsToConvertList[0]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created an extension method over IQueryable type, which takes a subset of entities
I created a method on a micropost model which takes an array of user_ids.
I have created a method which takes generate random numbers with the condition that
I have a general routine, which takes a few parameters. Something like: -(id) doStuff:(int)A:(int)B:(int)C
I have created code which takes a string from a text field and calls
I created an App which uses Timer class to callback a method at a
I created a constructor with Javassist which has no real method CtConstructor c =
I've created a singleton class which uses GetInstance() method to get the instance address
I have created a Webpage which will post as post method..not as get method.
I have created a custom shipping method to show depo-names from which customers will

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.