Quellcode durchsuchen

Fix #642130.

	* MonoGenericClass.cs (.ctor): Only register with the runtime
	when the underlying TB is not finished.

	* MonoGenericClassTest.cs: Add regression test.

	* TypeTest.cs: Fix a test that had a broken assumption.

	Fix #642130
Rodrigo Kumpera vor 15 Jahren
Ursprung
Commit
ef4cf45281

+ 11 - 1
mcs/class/corlib/System.Reflection/MonoGenericClass.cs

@@ -71,7 +71,17 @@ namespace System.Reflection
 		{
 			this.generic_type = tb;
 			this.type_arguments = args;
-			register_with_runtime (this); /*Temporary hack while*/
+			/*
+			This is a temporary hack until we can fix the rest of the runtime
+			to properly handle this class to be a complete UT.
+
+			We must not regisrer this with the runtime after the type is created
+			otherwise created_type.MakeGenericType will return an instance of MonoGenericClass,
+			which is very very broken.
+			*/
+			if (tb is TypeBuilder && !(tb as TypeBuilder).is_created)
+				register_with_runtime (this);
+			
 		}
 
 

+ 18 - 0
mcs/class/corlib/Test/System.Reflection/MonoGenericClassTest.cs

@@ -186,6 +186,24 @@ namespace MonoTests.System.Reflection.Emit
 				Assert.Fail ("#13");
 			} catch (NotSupportedException) {  }
 		}
+
+		[Test]
+		public void ClassMustNotBeRegisteredAfterTypeBuilderIsFinished ()
+		{
+			TypeBuilder tb = module.DefineType ("foo.type");
+			tb.DefineGenericParameters ("T");
+
+			var c = tb.CreateType ();
+
+			var sreInst = tb.MakeGenericType (typeof (int));
+			var rtInst = c.MakeGenericType (typeof (int));
+
+			Assert.AreNotSame (sreInst, rtInst, "#1");
+
+			/*This must not throw*/
+			rtInst.IsDefined (typeof (int), true);
+		}
 	}
+
 #endif
 }

+ 4 - 6
mcs/class/corlib/Test/System/TypeTest.cs

@@ -2961,13 +2961,11 @@ PublicKeyToken=b77a5c561934e089"));
 		public void MakeGenericType_BadUserType ()
 		{
 			Type ut = new UserType (null);
-			try {
-				Type t = typeof (Foo<>).MakeGenericType (ut);
-				Assert.Fail ("#1");
-			} catch (ArgumentException) {
-			}
+			Type t = typeof (Foo<>).MakeGenericType (ut);
+			var g0 = t.GetGenericArguments () [0];
+			Assert.AreSame (g0, ut, "#1");
 		}
-	
+
 		[Test]
 		public void MakeGenericType_WrongNumOfArguments ()
 		{