Browse Source

* TypeBuilderTest.cs: Renamed tests and updated bug number. Enabled
test for UnderlyingSystemType, and improved it. Improved test for
null parent.
* ConstructorOnTypeBuilderInstTest.cs: Enabled test for GetParameters.
* ConstructorBuilderTest.cs: Enabled test for GetParameters. Renamed
test for SetCustomAttribute, and added new tests.
* ModuleBuilderTest.cs: Added tests for bug #387404.
* TypeBuilder.cs: Use Assembly.FullName instead of AssemblyName.
In UnderlyingSystemType, return UnderlyingSystemType of created type
when available. For an enum, UnderlyingSystemType is the type of the
first instance field. If no instance field is available, throw an
InvalidOperationException. Retain original behavior when operating in
compiler context.
* ConstructorBuilder.cs: Moved implementation of GetParameters to
GetParametersInternal to allow it to be used internally when type is
not yet created.
* CustomAttributeBuilder.cs: Added GetParameters method that uses
GetParametersInternal on ConstructorBuilder, and use it where
necessary.

svn path=/trunk/mcs/; revision=102970

Gert Driesen 17 years ago
parent
commit
68c67fcf27

+ 15 - 0
mcs/class/corlib/System.Reflection.Emit/ChangeLog

@@ -1,3 +1,18 @@
+2008-05-11  Gert Driesen  <[email protected]>
+
+	* TypeBuilder.cs: Use Assembly.FullName instead of AssemblyName.
+	In UnderlyingSystemType, return UnderlyingSystemType of created type
+	when available. For an enum, UnderlyingSystemType is the type of the
+	first instance field. If no instance field is available, throw an
+	InvalidOperationException. Retain original behavior when operating in
+	compiler context.
+	* ConstructorBuilder.cs: Moved implementation of GetParameters to
+	GetParametersInternal to allow it to be used internally when type is
+	not yet created.
+	* CustomAttributeBuilder.cs: Added GetParameters method that uses
+	GetParametersInternal on ConstructorBuilder, and use it where
+	necessary.	
+
 2008-05-03  Zoltan Varga  <[email protected]>
 
 	* ConstructorBuilder.cs: Revert the throw not_created () change as it causes

+ 7 - 5
mcs/class/corlib/System.Reflection.Emit/ConstructorBuilder.cs

@@ -112,19 +112,21 @@ namespace System.Reflection.Emit {
 
 		public override ParameterInfo[] GetParameters ()
 		{
-			/* This causes problems when it is called from CustomAttributeBuilder.Initialize () */
-			/*
 			if (!type.is_created && !IsCompilerContext)
 				throw not_created ();
-			*/
 
+			return GetParametersInternal ();
+		}
+
+		internal ParameterInfo [] GetParametersInternal ()
+		{
 			if (parameters == null)
 				return new ParameterInfo [0];
 
-			ParameterInfo[] retval = new ParameterInfo [parameters.Length];
+			ParameterInfo [] retval = new ParameterInfo [parameters.Length];
 			for (int i = 0; i < parameters.Length; i++)
 				retval [i] = new ParameterInfo (pinfo == null ? null
-					: pinfo [i+1], parameters [i], this, i + 1);
+					: pinfo [i + 1], parameters [i], this, i + 1);
 
 			return retval;
 		}

+ 1 - 1
mcs/class/corlib/System.Reflection.Emit/ConstructorOnTypeBuilderInst.cs

@@ -166,4 +166,4 @@ namespace System.Reflection.Emit
 	}
 }
 
-#endif
+#endif

+ 11 - 3
mcs/class/corlib/System.Reflection.Emit/CustomAttributeBuilder.cs

@@ -167,7 +167,7 @@ namespace System.Reflection.Emit {
 			}
 
 			i = 0;
-			foreach (ParameterInfo pi in con.GetParameters ()) {
+			foreach (ParameterInfo pi in GetParameters (con)) {
 				if (pi != null) {
 					Type paramType = pi.ParameterType;
 					if (!IsValidType (paramType))
@@ -225,7 +225,7 @@ namespace System.Reflection.Emit {
 			utype = (int)data [2];
 			utype |= ((int)data [3]) << 8;
 
-			string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
+			string first_type_name = GetParameters (customBuilder.Ctor) [0].ParameterType.FullName;
 			int pos = 6;
 			if (first_type_name == "System.Int16")
 				pos = 4;
@@ -389,7 +389,7 @@ namespace System.Reflection.Emit {
 				throw new Exception ("Prolog invalid");
 			pos = 2;
 
-			ParameterInfo [] pi = ctor.GetParameters ();
+			ParameterInfo [] pi = GetParameters (ctor);
 			info.ctor = ctor;
 			info.ctorArgs = new object [pi.Length];
 			for (int i = 0; i < pi.Length; ++i)
@@ -457,5 +457,13 @@ namespace System.Reflection.Emit {
 		{
 			throw new NotImplementedException ();
 		}
+
+		static ParameterInfo [] GetParameters (ConstructorInfo ctor)
+		{
+			ConstructorBuilder cb = ctor as ConstructorBuilder;
+			if (cb != null)
+				return cb.GetParametersInternal ();
+			return ctor.GetParameters ();
+		}
 	}
 }

+ 23 - 2
mcs/class/corlib/System.Reflection.Emit/TypeBuilder.cs

@@ -126,7 +126,7 @@ namespace System.Reflection.Emit
 			this.class_size = type_size;
 			this.packing_size = packing_size;
 			this.nesting_type = nesting_type;
-				
+
 			check_name ("fullname", name);
 
 			if (parent == null && (attr & TypeAttributes.Interface) != 0 && (attr & TypeAttributes.Abstract) == 0)
@@ -158,7 +158,7 @@ namespace System.Reflection.Emit
 
 		public override string AssemblyQualifiedName {
 			get {
-				return fullname + ", " + Assembly.GetName().FullName;
+				return fullname + ", " + Assembly.FullName;
 			}
 		}
 
@@ -190,6 +190,21 @@ namespace System.Reflection.Emit
 
 		public override Type UnderlyingSystemType {
 			get {
+				if (is_created)
+					return created.UnderlyingSystemType;
+
+				if (IsEnum && !IsCompilerContext) {
+					for (int i = 0; i < num_fields; i++) {
+						FieldBuilder field = fields [i];
+						if ((field.Attributes & FieldAttributes.Static) == 0) {
+							return field.FieldType;
+						}
+					}
+
+					throw new InvalidOperationException (
+						"Enumeration type is not defined.");
+				}
+
 				return this;
 			}
 		}
@@ -1601,6 +1616,12 @@ namespace System.Reflection.Emit
 			return created.GetInterfaceMap (interfaceType);
 		}
 
+		internal bool IsCompilerContext {
+			get {
+				return ((AssemblyBuilder) Assembly).IsCompilerContext;
+			}
+		}
+
 		internal bool is_created {
 			get {
 				return created != null;

+ 10 - 0
mcs/class/corlib/Test/System.Reflection.Emit/ChangeLog

@@ -1,3 +1,13 @@
+2008-05-11  Gert Driesen  <[email protected]>
+
+	* TypeBuilderTest.cs: Renamed tests and updated bug number. Enabled
+	test for UnderlyingSystemType, and improved it. Improved test for
+	null parent.
+	* ConstructorOnTypeBuilderInstTest.cs: Enabled test for GetParameters.
+	* ConstructorBuilderTest.cs: Enabled test for GetParameters. Renamed
+	test for SetCustomAttribute, and added new tests.	
+	* ModuleBuilderTest.cs: Added tests for bug #387404.
+
 2008-05-08 Rodrigo Kumpera  <[email protected]>
 
 	* TypeBuilderTest.cs: Added regression tests for #354047.

+ 62 - 14
mcs/class/corlib/Test/System.Reflection.Emit/ConstructorBuilderTest.cs

@@ -397,7 +397,6 @@ public class ConstructorBuilderTest
 	}
 
 	[Test]
-	[Category ("NotWorking")]
 	public void GetParameters_Incomplete ()
 	{
 		ConstructorBuilder cb = genClass.DefineConstructor (
@@ -611,24 +610,13 @@ public class ConstructorBuilderTest
 		}
 	}
 
-	[Test]
-	public void TestSetCustomAttribute ()
+	[Test] // SetCustomAttribute (ConstructorInfo, Byte [])
+	public void SetCustomAttribute1 ()
 	{
 		ConstructorBuilder cb = genClass.DefineConstructor (
 			 0, 0, new Type [1] {typeof(int)});
 		cb.GetILGenerator ().Emit (OpCodes.Ret);
 
-		// Null argument
-		try {
-			cb.SetCustomAttribute (null);
-			Assert.Fail ("#A1");
-		} catch (ArgumentNullException ex) {
-			Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
-			Assert.IsNull (ex.InnerException, "#A3");
-			Assert.IsNotNull (ex.Message, "#A4");
-			Assert.AreEqual ("customBuilder", ex.ParamName, "#A5");
-		}
-
 		byte[] custAttrData = { 1, 0, 0, 0, 0};
 		Type attrType = Type.GetType
 			("System.Reflection.AssemblyKeyNameAttribute");
@@ -661,6 +649,66 @@ public class ConstructorBuilderTest
 		}
 	}
 
+	[Test] // SetCustomAttribute (CustomAttributeBuilder)
+	public void SetCustomAttribute2 ()
+	{
+		ConstructorBuilder cb = genClass.DefineConstructor (
+			MethodAttributes.Public, CallingConventions.Standard,
+			new Type [1] { typeof (int) });
+		cb.GetILGenerator ().Emit (OpCodes.Ret);
+
+		TypeBuilder attrTb = module.DefineType ("TestAttribute",
+			TypeAttributes.Public, typeof (Attribute));
+		ConstructorBuilder attrCb = attrTb.DefineDefaultConstructor (
+			MethodAttributes.Public);
+
+		CustomAttributeBuilder cab = new CustomAttributeBuilder (
+			attrCb, new object [0]);
+		cb.SetCustomAttribute (cab);
+		attrTb.CreateType ();
+		
+		Type emittedType  = genClass.CreateType ();
+		ConstructorInfo ci = emittedType.GetConstructor (
+			new Type [1] { typeof (int) });
+
+		Assert.IsNotNull (ci, "#1");
+		object [] cas = ci.GetCustomAttributes (false);
+		Assert.IsNotNull (cas, "#2");
+		Assert.AreEqual (1, cas.Length, "#3");
+		Assert.AreEqual ("TestAttribute", cas [0].GetType ().FullName, "#4");
+	}
+
+	[Test]
+	public void SetCustomAttribute2_CustomBuilder_Null ()
+	{
+		ConstructorBuilder cb = genClass.DefineConstructor (
+			MethodAttributes.Public, CallingConventions.Standard,
+			new Type [1] { typeof (int) });
+		cb.GetILGenerator ().Emit (OpCodes.Ret);
+
+		try {
+			cb.SetCustomAttribute ((CustomAttributeBuilder) null);
+			Assert.Fail ("#A1");
+		} catch (ArgumentNullException ex) {
+			Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
+			Assert.IsNull (ex.InnerException, "#A3");
+			Assert.IsNotNull (ex.Message, "#A4");
+			Assert.AreEqual ("customBuilder", ex.ParamName, "#A5");
+		}
+
+		genClass.CreateType ();
+
+		try {
+			cb.SetCustomAttribute ((CustomAttributeBuilder) null);
+			Assert.Fail ("#B1");
+		} catch (ArgumentNullException ex) {
+			Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
+			Assert.IsNull (ex.InnerException, "#B3");
+			Assert.IsNotNull (ex.Message, "#B4");
+			Assert.AreEqual ("customBuilder", ex.ParamName, "#B5");
+		}
+	}
+
 	[Test]
 	public void GetCustomAttributes_Emitted ()
 	{

+ 0 - 1
mcs/class/corlib/Test/System.Reflection.Emit/ConstructorOnTypeBuilderInstTest.cs

@@ -203,7 +203,6 @@ namespace MonoTests.System.Reflection.Emit
 		}
 
 		[Test]
-		[Category ("NotWorking")]
 		public void GetParameters ()
 		{
 			try {

+ 52 - 0
mcs/class/corlib/Test/System.Reflection.Emit/ModuleBuilderTest.cs

@@ -330,5 +330,57 @@ namespace MonoTests.System.Reflection.Emit
 			types = mb.GetTypes ();
 			Assert.AreEqual (tb1.CreateType (), types [0]);
 		}
+
+		[Test] // GetTypeToken (Type)
+#if NET_2_0
+		[Category ("NotDotNet")] // http://support.microsoft.com/kb/950986
+#endif
+		public void GetTypeToken2_Type_Array ()
+		{
+			Type type;
+			TypeToken typeToken;
+			Type resolved_type;
+
+			AssemblyName aname = genAssemblyName ();
+			AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
+				aname, AssemblyBuilderAccess.RunAndSave);
+			ModuleBuilder mb = ab.DefineDynamicModule ("MyModule");
+
+			type = typeof (object []);
+			typeToken = mb.GetTypeToken (type);
+#if NET_2_0
+			Assert.IsFalse (typeToken == TypeToken.Empty, "#A1");
+			resolved_type = mb.ResolveType (typeToken.Token);
+			Assert.AreEqual (type, resolved_type, "#A2");
+#else
+			Assert.IsFalse (typeToken.Token == TypeToken.Empty.Token, "#A1");
+#endif
+
+#if NET_2_0
+			type = typeof (object).MakeArrayType ();
+			typeToken = mb.GetTypeToken (type);
+			Assert.IsFalse (typeToken == TypeToken.Empty, "#B1");
+			resolved_type = mb.ResolveType (typeToken.Token);
+			Assert.AreEqual (type, resolved_type, "#B2");
+#endif
+		}
+
+		[Test] // GetTypeToken (Type)
+		public void GetTypeToken2_Type_String ()
+		{
+			AssemblyName aname = genAssemblyName ();
+			AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
+				aname, AssemblyBuilderAccess.RunAndSave);
+			ModuleBuilder mb = ab.DefineDynamicModule ("MyModule");
+			Type type = typeof (string);
+			TypeToken typeToken = mb.GetTypeToken (type);
+#if NET_2_0
+			Assert.IsFalse (typeToken == TypeToken.Empty, "#1");
+			Type resolved_type = mb.ResolveType (typeToken.Token);
+			Assert.AreEqual (type, resolved_type, "#2");
+#else
+			Assert.IsFalse (typeToken.Token == TypeToken.Empty.Token, "#1");
+#endif
+		}
 	}
 }

+ 70 - 32
mcs/class/corlib/Test/System.Reflection.Emit/TypeBuilderTest.cs

@@ -252,8 +252,8 @@ namespace MonoTests.System.Reflection.Emit
 #endif
 		}
 
-		[Test] //bug 82018
-		public void TestEnumWithoutValueFieldThrowsException ()
+		[Test] // bug #324692
+		public void CreateType_Enum_NoInstanceField ()
 		{
 			TypeBuilder tb = module.DefineType (genTypeName (),
 				TypeAttributes.Sealed | TypeAttributes.Serializable,
@@ -270,7 +270,7 @@ namespace MonoTests.System.Reflection.Emit
 #endif
 		}
 
-		[Test] // bug 82018
+		[Test] // bug #324692
 #if ONLY_1_1
 		[Category ("NotWorking")] // we do not throw IOE when repeatedly invoking CreateType
 #endif
@@ -1064,33 +1064,60 @@ namespace MonoTests.System.Reflection.Emit
 		}
 
 		[Test]
-		[Category ("NotWorking")]
-		public void TestUnderlyingSystemType ()
+		public void UnderlyingSystemType ()
 		{
-			{
-				TypeBuilder tb = module.DefineType (genTypeName ());
-				Assert.AreEqual (tb, tb.UnderlyingSystemType, "#1");
-			}
-			{
-				TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
-				Assert.AreEqual (tb, tb.UnderlyingSystemType, "#2");
-			}
-			{
-				TypeBuilder tb = module.DefineType (genTypeName (), 0, typeof (ValueType));
-				Assert.AreEqual (tb, tb.UnderlyingSystemType, "#3");
-			}
+			TypeBuilder tb;
+			Type emitted_type;
 
-			{
-				TypeBuilder tb = module.DefineType (genTypeName (), 0, typeof (Enum));
-				try {
-					Type t = tb.UnderlyingSystemType;
-					Assert.Fail ("#4");
-				} catch (InvalidOperationException) {
-				}
+			tb = module.DefineType (genTypeName ());
+			Assert.AreSame (tb, tb.UnderlyingSystemType, "#A1");
+			emitted_type = tb.CreateType ();
+			Assert.AreSame (emitted_type, tb.UnderlyingSystemType, "#A2");
+
+			tb = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
+			Assert.AreSame (tb, tb.UnderlyingSystemType, "#B1");
+			emitted_type = tb.CreateType ();
+			Assert.AreSame (emitted_type, tb.UnderlyingSystemType, "#B2");
 
-				tb.DefineField ("val", typeof (int), 0);
-				Assert.AreEqual (typeof (int), tb.UnderlyingSystemType, "#5");
+			tb = module.DefineType (genTypeName (), 0, typeof (ValueType));
+			Assert.AreSame (tb, tb.UnderlyingSystemType, "#C1");
+			emitted_type = tb.CreateType ();
+			Assert.AreSame (emitted_type, tb.UnderlyingSystemType, "#C2");
+
+			tb = module.DefineType (genTypeName (), 0, typeof (Enum));
+			try {
+				Type t = tb.UnderlyingSystemType;
+				Assert.Fail ("#D1:" + t);
+			} catch (InvalidOperationException ex) {
+				// Underlying type information on enumeration
+				// is not specified
+				Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
+				Assert.IsNull (ex.InnerException, "#D3");
+				Assert.IsNotNull (ex.Message, "#D4");
 			}
+			tb.DefineField ("val", typeof (int), FieldAttributes.Private);
+			Assert.AreEqual (typeof (int), tb.UnderlyingSystemType, "#D5");
+			emitted_type = tb.CreateType ();
+			Assert.AreSame (emitted_type, tb.UnderlyingSystemType, "#D6");
+
+			tb = module.DefineType (genTypeName (), 0, typeof (Enum));
+			tb.DefineField ("val", typeof (int), FieldAttributes.Static);
+			try {
+				Type t = tb.UnderlyingSystemType;
+				Assert.Fail ("#E1:" + t);
+			} catch (InvalidOperationException ex) {
+				// Underlying type information on enumeration
+				// is not specified
+				Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
+				Assert.IsNull (ex.InnerException, "#E3");
+				Assert.IsNotNull (ex.Message, "#E4");
+			}
+			tb.DefineField ("foo", typeof (long), FieldAttributes.Private);
+			Assert.AreEqual (typeof (long), tb.UnderlyingSystemType, "#E5");
+			tb.DefineField ("bar", typeof (short), FieldAttributes.Private);
+			Assert.AreEqual (typeof (long), tb.UnderlyingSystemType, "#E6");
+			tb.DefineField ("boo", typeof (int), FieldAttributes.Static);
+			Assert.AreEqual (typeof (long), tb.UnderlyingSystemType, "#E7");
 		}
 
 		[Test]
@@ -8510,6 +8537,8 @@ namespace MonoTests.System.Reflection.Emit
 				typeBuilder.CreateType ();
 				Assert.Fail ("#1");
 			} catch (TypeLoadException) {
+				// Could not load type '...' from assembly
+				// 'MonoTests.System.Reflection.Emit.TypeBuilderTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
 			}
 #if NET_2_0
 			Assert.IsTrue (typeBuilder.IsCreated (), "#2");
@@ -8650,7 +8679,7 @@ namespace MonoTests.System.Reflection.Emit
 		}
 
 		[Test]
-		public void EmptyMethodBody ()
+		public void CreateType_EmptyMethodBody ()
 		{
 			TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
 
@@ -8666,7 +8695,7 @@ namespace MonoTests.System.Reflection.Emit
 		}
 
 		[Test]
-		public void EmptyCtorBody ()
+		public void CreateType_EmptyCtorBody ()
 		{
 			TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
 
@@ -8682,12 +8711,21 @@ namespace MonoTests.System.Reflection.Emit
 		}
 
 		[Test]
-		public void ParentNull ()
+		public void CreateType_ParentNull ()
 		{
-			TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public, null);
-			Type t = tb.CreateType ();
+			TypeBuilder tb;
+			Type emitted_type;
+			
+			tb = module.DefineType (genTypeName (), TypeAttributes.Public, null);
+			// bug #389171:
+			//Assert.AreEqual (typeof (object), tb.BaseType, "#A1");
+			emitted_type = tb.CreateType ();
+			Assert.AreEqual (typeof (object), emitted_type.BaseType, "#A2");
 
-			Assert.AreEqual (typeof (object), t.BaseType);
+			tb = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract, null);
+			Assert.IsNull (tb.BaseType, "#B1");
+			emitted_type = tb.CreateType ();
+			Assert.IsNull (emitted_type.BaseType, "#B2");
 		}
 
 #if NET_2_0