[Mono-bugs] [Bug 60474][Blo] New - TypeBuilder.SetParent doesnt work for in memory assemblies

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Sat, 19 Jun 2004 21:48:32 -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 rbo@acm.org.

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

--- shadow/60474	2004-06-19 21:48:32.000000000 -0400
+++ shadow/60474.tmp.18906	2004-06-19 21:48:32.000000000 -0400
@@ -0,0 +1,79 @@
+Bug#: 60474
+Product: Mono: Class Libraries
+Version: unspecified
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Blocker
+Component: CORLIB
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: rbo@acm.org               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: TypeBuilder.SetParent doesnt work for in memory assemblies
+
+<snip>
+namespace MonoTestFixtures
+{
+	using System;
+	using System.Reflection;
+	using System.Reflection.Emit;
+	using NUnit.Framework;
+
+	public class ScriptBase
+	{
+		public virtual object Eval()
+		{
+			return null;
+		}
+	}
+	
+	[TestFixture]
+	public class TypeBuilderFixture
+	{
+		Type _type;
+
+		[SetUp]
+		public void SetUp()
+		{
+			AssemblyName assemblyName = new AssemblyName();
+			assemblyName.Name = "Temp";
+			
+			AssemblyBuilder assemblyBuilder =
+AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName,
+AssemblyBuilderAccess.Run);
+			ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("Temp");
+			TypeBuilder builder = moduleBuilder.DefineType("Script");
+			builder.SetParent(typeof(ScriptBase));
+			
+			MethodBuilder method = builder.DefineMethod("Eval",
+MethodAttributes.Public|MethodAttributes.Virtual, typeof(object), new Type[0]);
+			ILGenerator il = method.GetILGenerator();
+			il.Emit(OpCodes.Ldstr, "Hello, world!");
+			il.Emit(OpCodes.Ret);
+			
+			_type = builder.CreateType();
+		}
+		
+		[Test]
+		public void TestBaseType()
+		{
+			Assert.AreSame(typeof(ScriptBase), _type.BaseType);
+		}
+		
+		[Test]
+		public void TestCastToBaseType()
+		{
+			ScriptBase script = (ScriptBase)Activator.CreateInstance(_type);
+			Assert.AreEqual("Hello, world!", script.Eval());
+		}
+	}
+}
+</snip>
+
+How often does this happen? 
+Always.