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

  • SEARCH
  • Home
  • 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 6710817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:03:59+00:00 2026-05-26T08:03:59+00:00

I generate dynamic enums that represent integer IDs from my database in a C#

  • 0

I generate dynamic enums that represent integer IDs from my database in a C# ASP.NET solution. I would like two things, although neither may be possible.

1) I want the .ToString() method to give me “345” for example, not the string name of the enum (the int it represents as a string). Every answer to this question seems to be adding

[Description="Blah"]
EnumName = 1

above the declaration and using a GetDescription() method. I have no idea how to do this with the dynamic code that I am using.

2) I’d rather not cast to an int to use it as such, I’d rather (Enum.Name == 5) for example. If this isn’t possible I’ll cast, but I really don’t want to use ((int)Enum.Name)).ToString();

Here’s the dynamic code generation:

public static void Main()
{
    AppDomain domain = AppDomain.CurrentDomain;

    AssemblyName aName = new AssemblyName("DynamicEnums");
    AssemblyBuilder ab = domain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);

    ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");

    List<Type> types = new List<Type>();

    foreach(ReferenceType rt in GetTypes())
    {
        EnumBuilder eb = mb.DefineEnum(rt.Name, TypeAttributes.Public, typeof(int));

        foreach (Reference r in GetReferences(rt.ID))
        {
            eb.DefineLiteral(NameFix(r.Name), r.ID);
        }

        types.Add(eb.CreateType());
    }

    ab.Save(aName.Name + ".dll");

    foreach (Type t in types)
    {
        foreach (object o in Enum.GetValues(t))
        {
            Console.WriteLine("{0}.{1} = {2}", t, o, ((int) o));
        }

        Console.WriteLine();
        //Console.ReadKey();
    }

    Console.WriteLine();
    Console.WriteLine("Dynamic Enums Built Successfully.");
}

public static string NameFix(string name)
{
    //Strip all non alphanumeric characters
    string r = Regex.Replace(name, @"[^\w]", "");

    //Enums cannot begin with a number
    if (Regex.IsMatch(r, @"^\d"))
        r = "N" + r;

    return r;
}

There may just be no way to do what I want to do, and I’ll be stuck using:

(int)Countries.USA //For int value
((int)Countries.CAN).ToString() //For string representation of int value, ex. "354"

Any ideas?

  • 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-26T08:03:59+00:00Added an answer on May 26, 2026 at 8:03 am

    Could you adapt the type-safe enum pattern to do what you need?

    public class MyEnum
    {
        #region Enum Values
    
        // Pre defined values.    
        public static readonly MyEnum ValueOne = new MyEnum(0);
        public static readonly MyEnum ValueTwo = new MyEnum(1);
    
        // All values in existence.
        private static readonly Dictionary<int, MyEnum> existingEnums = new Dictionary<int, MyEnum>{{ValueOne.Value, ValueOne}, {ValueTwo.Value, ValueTwo}};
    
        #endregion
    
        #region Enum Functionality
    
        private readonly int Value;
    
        private MyEnum(int value)
        {
            Value = value;
        }
    
        public static MyEnum GetEnum(int value)
        {
            // You will probably want to make this thread-safe.
            if (!existingEnums.ContainsKey(value)) existingEnums[value] = new MyEnum(value);
    
            return existingEnums[value];
        }
    
        public override string ToString()
        {
            return Value.ToString();
        }
    
        #endregion
    }
    

    Usage:

    private void Foo(MyEnum enumVal)
    {
      return "Enum Value: " + enumVal;  // returns "Enum Value: (integer here) 
    }
    

    Or:

    MyEnum.GetValue(2) == MyEnum.GetValue(4); // false
    MyEnum.GetValue(3) == MyEnum.GetValue(3); // true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a reporting module in an ASP.NET webforms app (C#) that uses dynamic
how to generate dynamic tree.properties file from database and pass it to tree structure
i would like to generate dynamic tabs. so anchor tags will not have id
I have an application that uses window.open() to generate dynamic popups. Unfortunately, I've had
I generate a dynamic assembly with CSharpCodeProvider, from a C# source saved into a
I'm looking for a better way to generate dynamic html from php. Before I
say i have 2 asp.net default calendars. Now from one i select the date
How do I generate a label ID dynamically in ASP.NET MVC? I was able
i call my asp.net page method like below code $.ajax({ type: POST, url: MyPage.aspx/GetItems,
I am trying to generate dynamic menu according to the user permission given with

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.