L
Lawrence Oluyede
I have a list of strings and i'd like to build up an
enum from them... is there a way to do that?
Thanks in advance.
enum from them... is there a way to do that?
Thanks in advance.
Lawrence Oluyede said:is there a way to do that?
Richard A. Lowe said:You probably want to build the enums at compile time.. if you are using
Visual Studio you could generate them with a pre-build event. Otherwise
there's really no advantage to generating them, IMO.
What exactly is the issue? Are you getting any error message or just
need some background on using enums in general?
Lawrence Oluyede said:What exactly is the issue? Are you getting any error message or just
need some background on using enums in general?
NoI think that I'm able to use enums but I'm not able to create them
at runtime... here's some code to explain what i mean:
AppDomain domain = Thread.GetDomain();
AssemblyName name = new AssemblyName();
name.Name = "EnumAssembly";
AssemblyBuilder asmBuilder = domain.DefineDynamicAssembly(
name, AssemblyBuilderAccess.Run);
ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule("EnumModule");
EnumBuilder enumBuilder = modBuilder.DefineEnum("Language",
TypeAttributes.Public,
typeof(System.Int32));
for(int i = 0; i < al.Count; i++)
// here al is an array list with a list of string values
enumBuilder.DefineLiteral(al.ToString(), i);
Type enumType = enumBuilder.CreateType();
Enum enumObj = (Enum) Activator.CreateInstance(enumType);
// here is an example
enumType.GetField("ar-SA").SetValue(enumObj, 1);
What I'd like to is that when a dev use the class containing such code
could use enumObj like a normal enum AA { a, b, c }
Bye
// here is an example
try
{
enumType.GetField("en-US").SetValue(enumObj, 1);
}
catch( Exception e )
{
// Any exception generated is displayed.
Console.WriteLine( "Exception: {0}", e.Message );
}