Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want create a property for usercontrol with enum runtime .
i use below code for create enum runtime but i can`t create with that a property
for usercontrol in c# 2010


C#
       static AssemblyBuilder myAssemblyBuilder;
        static ModuleBuilder myModuleBuilder;
        static EnumBuilder myEnumBuilder;
        static void Main(string[] args)
        {
            try
            {
                CreateCallee(Thread.GetDomain(), AssemblyBuilderAccess.Save);
                Type[] myTypeArray = myModuleBuilder.GetTypes();
                foreach (Type myType in myTypeArray)
                {
 Console.WriteLine("Enum Builder defined in the module builder is: "+ myType.Name);
                }

                Console.WriteLine("Enum TypeToken is :" +
                                              myEnumBuilder.TypeToken.ToString());
                Console.WriteLine("Enum UnderLyingField is :" +
                                           myEnumBuilder.UnderlyingField.ToString());
                Console.WriteLine("Enum UnderLyingSystemType is :" +
                                     myEnumBuilder.UnderlyingSystemType.ToString());
                Console.WriteLine("Enum GUID is :" + myEnumBuilder.GUID.ToString());
                myAssemblyBuilder.Save("EmittedAssembly.dll");
                Console.Write(myEnumBuilder.GetEnumName(3));
            }
            catch (NotSupportedException ex)
            {
Console.WriteLine("The following is the exception is raised: " + ex.Message);
            }
            catch (Exception e)
            {
Console.WriteLine("The following is the exception raised: " + e.Message);
            }
            Console.ReadLine();
        }

        private static void CreateCallee(AppDomain myAppDomain, AssemblyBuilderAccess access)
        {
            // Create a name for the assembly.
            AssemblyName myAssemblyName = new AssemblyName();
            myAssemblyName.Name = "EmittedAssembly";

            // Create the dynamic assembly.
            myAssemblyBuilder = myAppDomain.DefineDynamicAssembly(myAssemblyName,
                                                   AssemblyBuilderAccess.Save);
            // Create a dynamic module.
            myModuleBuilder =myAssemblyBuilder.DefineDynamicModule("EmittedModule",
                                                "EmittedModule.mod");
            // Create a dynamic Enum.
            myEnumBuilder = myModuleBuilder.DefineEnum("MyNamespace.MyEnum",
                                       TypeAttributes.Public, typeof(Int32));

        FieldBuilder myFieldBuilder1 = myEnumBuilder.DefineLiteral("FieldOne", 1);
         FieldBuilder myFieldBuilder2 = myEnumBuilder.DefineLiteral("FieldTwo", 2);
            myEnumBuilder.DefineLiteral("test",3);

            myEnumBuilder.CreateType();
        }
Posted
Comments
BillWoodruff 18-Sep-11 3:13am    
Are you constructing these Enums from information in a database ?

I see two questions here: 1. about creating Enums at run-time, and, 2. How to define a property in a UserControl whose type is one of your created Enums ... correct ?

no it`s not for database.
and i want :define a property in a UserControl whose type is one of my created Enums and
that is a dynamic enum.
thanks for your help.
 
Share this answer
 
You wrote: "no it`s not for database." My question about the data for the internal values of the Enum was meant to find out if the values came from a database.

Check out "Enum Code Generator - Generating enum code automatically from database look up tables"[^].

And, this discussion on StackOverFlow may interest you:[^].

The question of how, once you have, at runtime, created a valid Enum you can then declare a property in a UserControl of the type of that Enum is a very interesting one.

My guess ... for what that's worth ... is that either you then have to use Reflection and dynamically modify the UserControl instance in memory at run-time, or you have use the 'dynamic' keyword in your UserControl so the Enum, is, essentially, "late bound."

But, as you may know, Enums are kind of special creatures, more value types than anything else, and whether you could use 'dynamic' in a UserControl, and get late-binding on an Enum ... sorry to say ... is beyond my skills, technically.

I'd be interested to know the answer to that one, also.

good luck, Bill
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900