Microsoft KB Archive/311092

= BUG: EnumBuilder Members Are Not Strongly Typed =

Article ID: 311092

Article Last Modified on 2/23/2007

-

APPLIES TO


 * Microsoft Common Language Runtime (included with the .NET Framework) 1.0
 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q311092



The following .NET Framework Class Library namespace is referenced in this article:
 * System.R



SYMPTOMS
When you use Reflection Emit to define an enum with ModuleBuilder.DefineEnum, you use EnumBuilder.DefineLiteral to define the members for the enum. The type of the defined members should be the same type as the enum. However, the type is int32.



CAUSE
A bug in the CLR causes the members to be of type int32.



RESOLUTION
To work around this problem, use the TypeBuilder class to define the enum and use the FieldBuilder class to define the members of the enum. The following steps demonstrate how to perform this workaround:  Create a new console application in C# .NET.  Replace all of the code in the module with the following code: using System; using System.Reflection; using System.Reflection.Emit; using System.Configuration.Assemblies;

class EnumBuilderWorkaround {   public static void Main { String g = &quot;MyAssembly&quot;; AssemblyName asmname = new AssemblyName; asmname.Name = g;

AssemblyBuilder asmbuild = System.Threading.Thread.GetDomain. DefineDynamicAssembly(asmname, AssemblyBuilderAccess.RunAndSave);  // Run and save only assembly.

ModuleBuilder mod = asmbuild.DefineDynamicModule(&quot;Mod1&quot;, asmname.Name + &quot;.dll&quot; );

TypeBuilder tb = mod.DefineType( &quot;myenum&quot;, TypeAttributes.Public | TypeAttributes.Sealed,                           typeof(Enum) ); tb.DefineField( &quot;value__&quot;, typeof(int),                   FieldAttributes.Private | FieldAttributes.SpecialName |                        FieldAttributes.RTSpecialName ); FieldBuilder fb = tb.DefineField( &quot;myfield1&quot;, tb,                   FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal );

fb.SetConstant( 1 );

tb.CreateType;

Console.WriteLine( asmname.Name + &quot;.dll&quot; ); asmbuild.Save( asmname.Name + &quot;.dll&quot; );

} }                    Save, build, and then execute the console application. View the Debug/Bin directory in the application directory. MyAssembly.dll is built and placed in this directory. Use Ildasm.exe to open MyAssembly.dll. Expand the myenum node. Note that myfield1 is of type myenum and not of type int32.</li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

Additional query words: kbreadme

Keywords: kbbug kbfix kbreadme KB311092

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.