本文介绍了用户控件属性的枚举运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用枚举运行时为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();
}
推荐答案
这篇关于用户控件属性的枚举运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!