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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:46:47+00:00 2026-05-14T22:46:47+00:00

What i am trying to achieve here is a bit tricky. Let me brief

  • 0

What i am trying to achieve here is a bit tricky. Let me brief on a little background first before going ahead.

I am aware that we can use a enum as a type to a parameter of a method. For example I can do something like this (a very basic example)

namespace Test
{
    class DefineEnums
    {
        public enum MyEnum
        {
            value1 = 0,
            value2 = 1
        }
    }
    class UseEnums
    {
        public void UseDefinedEnums(DefineEnums.MyEnum _enum)
        { 
            //Any code here.
        }

        public void Test()
        {
            // "value1" comes here with the intellisense.
            UseDefinedEnums(DefineEnums.MyEnum.value1);
        }
    }
}

What i need to do is create a dynamic Enum and use that as type in place of DefineEnums.MyEnum mentioned above.

I tried the following.
1. Used a method which i got from the net to create a dynamic enum from a list of strings. And created a static class which i can use.

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

namespace Test
{
    public static class DynamicEnum
    {

        public static Enum finished;
        static List<string> _lst = new List<string>();

        static DynamicEnum()
        {
            _lst.Add("value1");
            _lst.Add("value2");

            finished = CreateDynamicEnum(_lst);
        }

        public static Enum CreateDynamicEnum(List<string> _list)
        {
            // Get the current application domain for the current thread.
            AppDomain currentDomain = AppDomain.CurrentDomain;

            // Create a dynamic assembly in the current application domain, 
            // and allow it to be executed and saved to disk.
            AssemblyName aName = new AssemblyName("TempAssembly");
            AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(
                aName, AssemblyBuilderAccess.RunAndSave);

            // Define a dynamic module in "TempAssembly" assembly. For a single-
            // module assembly, the module has the same name as the assembly.
            ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");

            // Define a public enumeration with the name "Elevation" and an 
            // underlying type of Integer.
            EnumBuilder eb = mb.DefineEnum("Elevation", TypeAttributes.Public, typeof(int));

            // Define two members, "High" and "Low".
            //eb.DefineLiteral("Low", 0);
            //eb.DefineLiteral("High", 1);

            int i = 0;
            foreach (string item in _list)
            {
                eb.DefineLiteral(item, i);
                i++;
            }

            // Create the type and save the assembly.
            return (Enum)Activator.CreateInstance(eb.CreateType());
            //ab.Save(aName.Name + ".dll");


        }
    }
}
  1. Tried using the class but i am unable to find the “finished” enum defined above. i.e. I am not able to do the following
        public static void TestDynEnum(Test.DynamicEnum.finished _finished)
        {
            // Do anything here with _finished.
        }

I guess the post has become too long but i hope i have made it quite clear.

  • 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-14T22:46:48+00:00Added an answer on May 14, 2026 at 10:46 pm

    You’ll either have to pass in an object, or create your method dynamically as well.

    Might I ask why you can’t just use an int for this? Seems like you’re going to have to dynamically create a lot of code just to be able to pass it around as an enum.

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

Sidebar

Related Questions

Here's what I'm trying to achieve: I have a div that switches between fixed
Here are the goals I'm trying to achieve: I need to pack 32 bit
What I am trying to achieve is a form that has a button on
What I'm trying to achieve is to determine if the Postscript that I'm parsing
Here's what I'm trying to achieve. I have a directory with over 4200 sub-files/sub-directories,
I am trying to achieve simple effects here and there. Please look this code
What i'm trying to achieve here is a straight value comparison of boxed primitive
what I am trying to achieve here is echoing a list of 5 links,
this is what I am trying to achieve here. I have a login page
I am trying to achieve better performance for my Java SWT application, and I

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.