[Mono-bugs] [Bug 67760][Min] New - NullReferenceException when creating CustomAttributeBuilder utilizing runtime created attribute and runtime created enum type

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Thu, 7 Oct 2004 10:51:52 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by nazgul@omega.pl.

http://bugzilla.ximian.com/show_bug.cgi?id=67760

--- shadow/67760	2004-10-07 10:51:51.000000000 -0400
+++ shadow/67760.tmp.6287	2004-10-07 10:51:52.000000000 -0400
@@ -0,0 +1,147 @@
+Bug#: 67760
+Product: Mono: Class Libraries
+Version: unspecified
+OS: Mandrake 9.1
+OS Details: mono 1.0.1
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Minor
+Component: CORLIB
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: nazgul@omega.pl               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: NullReferenceException when creating CustomAttributeBuilder utilizing runtime created attribute and runtime created enum type
+
+Please fill in this template when reporting a bug, unless you know what you
+are doing.
+Description of Problem:
+NullReferenceException when creating CustomAttributeBuilder utilizing
+runtime created attribute and runtime created enum type (using DefineEnum
+methods, with DefineType it works ok)
+
+Steps to reproduce the problem:
+1. compile and run following program
+
+using System;
+using System.Threading;
+using System.Reflection;
+using System.Reflection.Emit;
+
+class MethodBuilderCustomAttributesDemo
+
+{
+
+   public static void BuildTypeWithCustomAttributesOnMethod()
+   {
+    
+    AppDomain currentDomain = Thread.GetDomain();
+    
+    AssemblyName myAsmName = new AssemblyName();
+    myAsmName.Name = "MyAssembly";
+
+    AssemblyBuilder myAsmBuilder = currentDomain.DefineDynamicAssembly(
+                       myAsmName, AssemblyBuilderAccess.Run);
+
+    ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule("MyModule");
+
+/*    
+    //// create enum type as type
+    TypeBuilder myEnumBuilder = myModBuilder.DefineType("MyEnum",
+                                  TypeAttributes.Public,typeof(System.Enum));
+
+    // Define the named static fields in the enumeration type 'MyEnum'.
+    FieldBuilder f = myEnumBuilder.DefineField("MyEnumMember1", typeof
+(int), FieldAttributes.Public |
+      FieldAttributes.Static | FieldAttributes.Literal);
+    f.SetConstant (1);
+    
+    myEnumBuilder.DefineField("value__", typeof (int),
+FieldAttributes.Public | 
+      FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);    
+
+    Type enumType = myEnumBuilder.CreateType();
+    ///// end create enum type as type                            
+*/    
+///*
+    // with this way of creating enum type it works
+    ///// create enum type by DefineEnum
+    EnumBuilder myEnumBuilder = myModBuilder.DefineEnum ("MyEnum",
+                                  TypeAttributes.Public, typeof (int));
+
+    // Define the named static fields in the enumeration type 'MyEnum'.
+    myEnumBuilder.DefineLiteral("MyEnumMember1", 1);
+
+    Type enumType = myEnumBuilder.CreateType();
+    ///// end create enum type by DefineEnum
+//*/
+
+    TypeBuilder myAttrBuilder = myModBuilder.DefineType("MyAttrType",
+                        TypeAttributes.Public, typeof (System.Attribute));
+
+    Type[] constructorArgs = { myEnumBuilder }; // this works only with
+DefineType
+    //Type[] constructorArgs = { enumType };  // this works in both cases
+    
+    ConstructorBuilder myConstructorBuilder = 
+    myAttrBuilder.DefineConstructor(MethodAttributes.Public, 
+                      CallingConventions.Standard, constructorArgs);
+    ILGenerator myConstructorIL = myConstructorBuilder.GetILGenerator();
+    myConstructorIL.Emit(OpCodes.Ret);
+    myAttrBuilder.CreateType ();
+
+    // we'll build a type with a custom attribute attached.
+    TypeBuilder myTypeBuilder = myModBuilder.DefineType("MyType",
+                        TypeAttributes.Public);
+
+    ConstructorInfo classCtorInfo = myConstructorBuilder;
+
+    /// creating CustomAttributeBuilder throws exception
+    CustomAttributeBuilder myCABuilder = new CustomAttributeBuilder(
+                        classCtorInfo,
+                        new object[] { 1 });
+
+    myTypeBuilder.SetCustomAttribute(myCABuilder);
+
+    myTypeBuilder.CreateType();
+   }
+
+   public static void Main() 
+   {
+
+    BuildTypeWithCustomAttributesOnMethod();
+   }
+}
+
+2. see the exception description
+3. 
+
+Actual Results:
+Unhandled Exception: System.NullReferenceException: Object reference not
+set to an instance of an object
+in (unmanaged) (wrapper managed-to-native) System.Type:type_is_subtype_of
+(System.Type,System.Type,bool)
+in <0x00004> (wrapper managed-to-native) System.Type:type_is_subtype_of
+(System.Type,System.Type,bool)
+in <0x00015> System.Type:get_IsEnum ()
+in <0x0095e> System.Reflection.Emit.CustomAttributeBuilder:Initialize
+(System.Reflection.ConstructorInfo,object[],System.Reflection.PropertyInfo[],object[],System.Reflection.FieldInfo[],object[])
+in <0x0008b> System.Reflection.Emit.CustomAttributeBuilder:.ctor
+(System.Reflection.ConstructorInfo,object[])
+in <0x00269>
+MethodBuilderCustomAttributesDemo:BuildTypeWithCustomAttributesOnMethod ()
+in <0x00007> MethodBuilderCustomAttributesDemo:Main ()
+
+
+Expected Results:
+clear run
+
+How often does this happen? 
+always
+
+Additional Information:
+MS.NET 1.1 and 2.0 have the similar bug when using DefineType to create
+enum type, while mono works fine with this.