Bugzilla – Bug 318438
[GMCS] NullRef inside BindGenericParameters when using Type from CreateType ()
Last modified: 2007-09-15 21:24:46 UTC
---- Reported by nazgul@omega.pl 2005-07-03 14:43:53 MST ---- Please fill in this template when reporting a bug, unless you know what you are doing. Description of Problem: There is a problem in binding generic parameters of System.Type returned from TypeBuilder.CreateType() Steps to reproduce the problem: 1. Compile and run program: using System; using System.Reflection; using System.Reflection.Emit; class M { static void Main () { /// INITIAL ASSEMBLY CREATION AssemblyName name = new AssemblyName(); name.Name = "o"; AssemblyBuilder builder = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndSave); ModuleBuilder mb = builder.DefineDynamicModule("out", "bla.dll"); /////////// /// abstract class Func ['a] /// { public abstract apply () : 'a; } TypeBuilder func = mb.DefineType("Func", TypeAttributes.Abstract); GenericTypeParameterBuilder[] func_gp = func.DefineGenericParameters("'a"); func.SetParent(typeof (object)); MethodBuilder func_apply = func.DefineMethod("apply", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, func_gp[0], new Type[] { }); Type func_c = func.CreateType(); /// class Prox : Func[string] { /// } // this causes NullRef in BindGenericParameters Type func_string = func_c.MakeGenericType(new Type [] { typeof(string)}); // and this causes InvalidOperation in GetGenericTypeDefinition() // Type func_string = func_c.GetGenericTypeDefinition ().MakeGenericType(new Type [] { typeof(string)}); TypeBuilder prox = mb.DefineType("Prox", TypeAttributes.Public); prox.SetParent(func_string); prox.CreateType(); builder.Save("bla.dll"); } } Actual Results: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object in <0x00000> <unknown method> in (wrapper managed-to-native) System.Type:BindGenericParameters (System.Type,System.Type[]) in <0x00068> System.Type:BindGenericParameters (System.Type[] types) in <0x00010> System.Type:MakeGenericType (System.Type[] types) in <0x0018d> M:Main () Expected Results: clear run and valid bla.dll created How often does this happen? Always Additional Information: Also GetGenericTypeDefinition do not work for type returned from CreateType(). It can be workarounded by using original TypeBuilder instance instead of one returned from CreateType, but it is important for us to have the created type also working, since some MS.NET bugs are triggered when using original TypeBuilder. I hope there is no reason why GetGenericTypeDefinition or BindGenericParameters should not work... ---- Additional Comments From martin@ximian.com 2005-07-11 12:25:31 MST ---- Fixed in SVN. ---- Additional Comments From nazgul@omega.pl 2005-07-11 13:13:01 MST ---- One more thing... When I change assembly attribute to Save only, it crashes again: using System; using System.Reflection; using System.Reflection.Emit; class M { static void Main () { /// INITIAL ASSEMBLY CREATION AssemblyName name = new AssemblyName(); name.Name = "o"; AssemblyBuilder builder = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Save); ModuleBuilder mb = builder.DefineDynamicModule("out", "bla.dll"); /////////// /// abstract class Func ['a] /// { public abstract apply () : 'a; } TypeBuilder func = mb.DefineType("Func", TypeAttributes.Abstract); GenericTypeParameterBuilder[] func_gp = func.DefineGenericParameters("'a"); func.SetParent(typeof (object)); MethodBuilder func_apply = func.DefineMethod("apply", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, func_gp[0], new Type[] { }); Type func_c = func.CreateType(); /// class Prox : Func[string] { /// } // this causes NullRef in BindGenericParameters Type func_string = func_c.MakeGenericType(new Type [] { typeof(string)}); // and this causes InvalidOperation in GetGenericTypeDefinition() //Type func_string = func_c.GetGenericTypeDefinition ().MakeGenericType(new Type [] { typeof(string)}); TypeBuilder prox = mb.DefineType("Prox", TypeAttributes.Public); prox.SetParent(func_string); prox.CreateType(); builder.Save("bla.dll"); } } ---- Additional Comments From martin@ximian.com 2005-07-12 08:42:39 MST ---- Fixed in SVN. Unknown bug field "cf_op_sys_details" encountered while moving bug <cf_op_sys_details>mono svn</cf_op_sys_details> Unknown operating system unknown. Setting to default OS "Other".