Hi there, System.Enum.ToObject() is your friend -- here's a simple example of (non-nested) use:
EnumBuilder enumBuilder = moduleBuilder.DefineEnum("SomeEnum", TypeAttributes.Public, typeof(long)); enumBuilder.DefineLiteral("One", (long)1); enumBuilder.DefineLiteral("Two", (long)2); Type enumTy = enumBuilder.CreateType(); object tag = Enum.ToObject(ty, (long)2); // Two TypeBuilder tyBuilder = moduleBuilder.DefineType("SomeClass", TypeAttributes.Public); FieldBuilder fiBuilder = tyBuilder.DefineField("SomeConstant", enumTy, FieldAttributes.Public | FieldAttributes.Literal | FieldAttributes.Static); fiBuilder.SetConstant(tag); ty = tyBuilder.CreateType(); hth --sigbjorn ----- Original Message ----- From: "Michał Jaeschke" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, October 22, 2002 01:42 Subject: [ADVANCED-DOTNET] emit literal field of enum type > I ask again. I'm trying emit this: > <----------------------------- > public class Test13_ConstLiteral > { > enum LiteralEnum : long > { > one, two > } > const Test13_ConstLiteral.LiteralEnum en = > Test13_ConstLiteral.LiteralEnum.two; > } > -----------------------------> > by using Refelction.Emit. It's not important if this Enum type is nested or > not. Problem is when: > - field is literal > - type of field is enum type > - this enum type is emitted in the same assembly > > Here is my pseudoCode: > <------------------------------- > .... > FieldBuilder fb = .... > object val = FieldInfo.GetValue(new object()) > TypeBuilder newTypeOfValue = xxx.defineType(..); > ... > newTypeOfValue.CreateType() > .. > string stringVal = val.ToString(); > fb.SetConstant(Enum.Parse(newTypeOfValue, stringVal)); > ......... > -------------------------------> > I've got exception (during SetConstant) > type of exception = System.ArgumentException > StackTrace = "at System.Enum.Parse(Type enumType, String value, Boolean > ignoreCase) > at System.Enum.Parse(Type enumType, String value) > at mycode.cs:line 240" > Message "Type must be a type provided by the runtime, not a TypeDelegator, > etc. Parameter name: enumType" > > Of course I invoke CreateType() on this new TypeBuilder() , but it's not > important if typebuilder.m_hasBeenCreated is true or false; > > Any idea how emit this literals? > You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.