Hi Lawrence,
I have taken some time to put together the example so that I could
gain a better understanding of what you are attempting to do -- so
here is what I saw (hope it matches your intent).
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
namespace EnumTest1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
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));
string[] al={"en-US","en-UK","ar-SA","da-DK","French","Cantonese"};
for(int i = 0; i < al.Length; i++)
{
// here al is an array list with a list of string values
enumBuilder.DefineLiteral(al[i].ToString(), i);
}
Type enumType = enumBuilder.CreateType();
Enum enumObj = (Enum) Activator.CreateInstance(enumType);
// 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 );
}
}
}
}
The only error I get is when we try to set the field value using the
enumObj (which is just an instance of the enum struct with the default
enum only)
The error is:
"Cannot set a final field"
I guess I'm not sure that you can set an enum in the same manner
as you'd set a field value (at least within the context of using this
function).
I see no reason why a developer could not legitimately use the
enums defined in the module that you created...
An alternative to trying to set the field of the object might be
to pass data between application domains using the SetData and GetData
methods of the AppDomain class (to pass the values for the Language
enum).
Good Luck,
~~~~~~~~~~~~~
Tommie Carter
www.premiertechnology.com
--
Lawrence Oluyede <ra***@dot.com> wrote in message news:<87************@mobile.foo>...
tc********@hotmail.com (Tom Carter) writes:
What exactly is the issue? Are you getting any error message or just
need some background on using enums in general?
No :) I 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[i].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