用户控件属性的枚举运行时 [英] enum runtime for usercontrol property

查看:68
本文介绍了用户控件属性的枚举运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用枚举运行时为usercontrol创建一个属性.
我使用下面的代码创建枚举运行时,但是我无法使用该属性创建
用于c#2010中的usercontrol


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


       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();
        }

推荐答案

不,它不适用于数据库.
而且我想:在UserControl中定义一个属性,该属性的类型是我创建的Enums之一,
那是一个动态的枚举.
感谢您的帮助.
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.


您写道:不,它不用于数据库."我关于枚举内部值的数据的问题旨在确定值是否来自数据库.

检出枚举代码生成器-从数据库查找表自动生成枚举代码" [ ^ ].

如何在运行时创建有效的Enum的问题,然后可以在该Enum类型的UserControl中声明一个属性是一个非常有趣的问题.

我的猜测……这是值得的……是您要么必须使用Reflection并在运行时动态修改内存中的UserControl实例,要么您必须在您的代码中使用"dynamic"关键字UserControl,因此Enum本质上是后期绑定".

但是,正如您可能知道的那样,枚举是一种特殊的生物,其值类型比其他任何事物都多,并且您是否可以在UserControl中使用动态"并在枚举上获得后期绑定...抱歉地说. ..在技术上超出了我的能力.

我也想知道这个答案.

Bill,祝你好运
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


这篇关于用户控件属性的枚举运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆