Click here to Skip to main content
15,900,816 members
Home / Discussions / C#
   

C#

 
AnswerRe: User Control Pin
Luc Pattyn21-May-10 2:30
sitebuilderLuc Pattyn21-May-10 2:30 
GeneralRe: User Control Pin
bubuzzz21-May-10 14:17
bubuzzz21-May-10 14:17 
GeneralRe: User Control Pin
Luc Pattyn21-May-10 14:42
sitebuilderLuc Pattyn21-May-10 14:42 
QuestionAccess Oracle database in Linux System through windows system. Pin
Narasimha Murthy SR21-May-10 0:00
Narasimha Murthy SR21-May-10 0:00 
AnswerQL Pin
Alex Manolescu21-May-10 0:41
Alex Manolescu21-May-10 0:41 
QuestionHow to check for different datbase errors Pin
Chiman120-May-10 23:43
Chiman120-May-10 23:43 
AnswerRe: How to check for different datbase errors Pin
PIEBALDconsult21-May-10 3:39
mvePIEBALDconsult21-May-10 3:39 
QuestionUsing dynamic enum as type in a parameter of a method Pin
dashingsidds20-May-10 23:25
dashingsidds20-May-10 23:25 
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));

            // 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");


        }
    }
}


2. 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.

Why i need such a functionality?

I can very well make the method with string as a parameter. I need this so that the developer who is using the method need not have to remember the set of values, amongst which only 1 he should pass. He will directly get it through the enum which is being created dynamically.

Anyone Please help!
Thanks in advance!

Regards,

Samar
AnswerRe: Using dynamic enum as type in a parameter of a method Pin
dashingsidds21-May-10 1:36
dashingsidds21-May-10 1:36 
AnswerRe: Using dynamic enum as type in a parameter of a method Pin
PIEBALDconsult21-May-10 3:46
mvePIEBALDconsult21-May-10 3:46 
AnswerRe: Using dynamic enum as type in a parameter of a method Pin
T M Gray21-May-10 5:19
T M Gray21-May-10 5:19 
QuestionDuplicate form with every instantiation of a class?? Pin
Jibrohni20-May-10 22:03
Jibrohni20-May-10 22:03 
AnswerRe: Duplicate form with every instantiation of a class?? Pin
OriginalGriff20-May-10 23:56
mveOriginalGriff20-May-10 23:56 
GeneralRe: Duplicate form with every instantiation of a class?? Pin
Jibrohni21-May-10 0:29
Jibrohni21-May-10 0:29 
GeneralRe: Duplicate form with every instantiation of a class?? Pin
Jibrohni21-May-10 0:47
Jibrohni21-May-10 0:47 
GeneralRe: Duplicate form with every instantiation of a class?? Pin
OriginalGriff21-May-10 1:09
mveOriginalGriff21-May-10 1:09 
GeneralRe: Duplicate form with every instantiation of a class?? Pin
Luc Pattyn21-May-10 2:42
sitebuilderLuc Pattyn21-May-10 2:42 
AnswerRe: Duplicate form with every instantiation of a class?? Pin
PIEBALDconsult21-May-10 4:28
mvePIEBALDconsult21-May-10 4:28 
QuestionSelectedValue and comboboxen Pin
redspiderke20-May-10 21:44
redspiderke20-May-10 21:44 
AnswerRe: SelectedValue and comboboxen Pin
V.20-May-10 23:10
professionalV.20-May-10 23:10 
GeneralSOVELD: SelectedValue and comboboxen Pin
redspiderke21-May-10 3:59
redspiderke21-May-10 3:59 
AnswerRe: SelectedValue and comboboxen Pin
Luc Pattyn21-May-10 2:48
sitebuilderLuc Pattyn21-May-10 2:48 
GeneralOPGELOST: SelectedValue and comboboxen Pin
redspiderke21-May-10 4:01
redspiderke21-May-10 4:01 
QuestionMessage Removed Pin
20-May-10 20:43
tanzeel8520-May-10 20:43 
AnswerRe: wmp..urgent Pin
J4amieC20-May-10 21:04
J4amieC20-May-10 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.