在Silverlight中动态创建Enum时,多次调用改方法出现上图所示错误,后来发现定义名称都是一样的,
在程序中声明全局变量去区别就可以了。
int num = 1;
private Type CreateEnum(List<string> lists,string enumName)
{
AppDomain app = AppDomain.CurrentDomain;
AssemblyName aName = new AssemblyName("SilverlightEnum" + num);
AssemblyBuilder ab = app.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name + "Module");
EnumBuilder eb = mb.DefineEnum(enumName, TypeAttributes.Public, typeof(System.Int32));
for (int i = 0; i < lists.Count; i++)
{
eb.DefineLiteral(lists[i], i);
}
num++;
return eb.CreateType();
}