Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using dynamic enum as type in a parameter of a method

Using dynamic enum as type in a parameter of a method

Scheduled Pinned Locked Moved C#
visual-studiotutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dashingsidds
    wrote on last edited by
    #1

    Hi Experts, 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));
    
            /
    
    D P T 3 Replies Last reply
    0
    • D dashingsidds

      Hi Experts, 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));
      
              /
      
      D Offline
      D Offline
      dashingsidds
      wrote on last edited by
      #2

      Hi Experts, Any suggestions on this?? Regards, Samar

      1 Reply Last reply
      0
      • D dashingsidds

        Hi Experts, 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));
        
                /
        
        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Will generics solve the problem?

        1 Reply Last reply
        0
        • D dashingsidds

          Hi Experts, 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));
          
                  /
          
          T Offline
          T Offline
          T M Gray
          wrote on last edited by
          #4

          Any time you get some weird code from somewhere the best place to get answers when it doesn't work is to ask in the same place you got the code. I don't really understand your goal. If your developers can't be relied upon to remember simple things then expecting them to use things like dynamic enums is probably a bad idea. Maybe if you gave an example of how it would be used we could give you an alternative. If the enum isn't built until runtime I don't see how it can give any advantage to someone at coding time.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups