private Type CreateEnum() { List<string> lists = new List<string>(); lists.Add("男"); lists.Add("女"); AppDomain app = AppDomain.CurrentDomain; AssemblyName aName = new AssemblyName("EnumAssemble"); AssemblyBuilder ab = app.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run); ModuleBuilder mb = ab.DefineDynamicModule("EnumModule"); EnumBuilder eb = mb.DefineEnum("Sexs", TypeAttributes.Public, typeof(System.Int32)); for (int i = 0; i < lists.Count; i++) { eb.DefineLiteral(lists[i], i); } return eb.CreateType(); }
注意如果要多次调用该方法,AssemblyName需要不同的名字,请参考 http://www.cnblogs.com/ZJ199012/p/4016434.html