2 // MonoGenericClassTest.cs - NUnit Test Cases for MonoGenericClassTest
4 // Rodrigo Kumpera <rkumpera@novell.com>
6 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
7 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
11 using System.Collections;
12 using System.Collections.Generic;
13 using System.Collections.ObjectModel;
14 using System.Threading;
15 using System.Reflection;
16 using System.Reflection.Emit;
18 using System.Security;
19 using System.Security.Permissions;
20 using System.Runtime.InteropServices;
21 using NUnit.Framework;
22 using System.Runtime.CompilerServices;
24 namespace MonoTests.System.Reflection.Emit
28 public class MonoGenericClassTest
30 AssemblyBuilder assembly;
33 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.MonoGenericClassTest";
37 return "internal__type"+ typeCount++;
43 SetUp (AssemblyBuilderAccess.RunAndSave);
46 void SetUp (AssemblyBuilderAccess access)
48 AssemblyName assemblyName = new AssemblyName ();
49 assemblyName.Name = ASSEMBLY_NAME;
52 Thread.GetDomain ().DefineDynamicAssembly (
53 assemblyName, access, Path.GetTempPath ());
55 module = assembly.DefineDynamicModule ("module1");
61 public void TestNameMethods ()
63 TypeBuilder tb = module.DefineType ("foo.type");
64 tb.DefineGenericParameters ("T", "K");
66 Type inst = tb.MakeGenericType (typeof (double), typeof (string));
68 Assert.AreEqual ("type", inst.Name, "#1");
69 Assert.AreEqual ("foo", inst.Namespace, "#2");
71 Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]", inst.FullName, "#3");
72 Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
74 Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]", inst.FullName, "#3");
75 Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
77 Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]", inst.FullName, "#3");
78 Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
80 Assert.AreEqual ("foo.type[System.Double,System.String]", inst.ToString (), "#5");
83 static void CheckInst (string prefix, Type inst, int a, int b)
85 var resA = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
86 var resB = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance);
88 Assert.AreEqual (a, resA.Length, prefix + 1);
89 Assert.AreEqual (b, resB.Length, prefix + 2);
93 [Category ("NotDotNet")]
94 // CompilerContext no longer supported
95 [Category ("NotWorking")]
96 public void GetMethodsWorkWithFunkyInstantiations ()
98 SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
99 TypeBuilder tb = module.DefineType ("Base", TypeAttributes.Public, typeof (object));
101 var a = typeof (IList<>).GetGenericArguments () [0];
102 var b = tb.DefineGenericParameters ("T") [0];
104 CheckInst ("#A", typeof (Collection<>).MakeGenericType (new Type [] {a}), 12, 16);
105 CheckInst ("#B", typeof (Collection<>).MakeGenericType (new Type[] { b }), 12, 16);
107 var tb2 = module.DefineType ("Child", TypeAttributes.Public, typeof (Collection<>).MakeGenericType (tb.MakeGenericType (typeof (int))));
108 tb2.DefineGenericParameters ("K");
110 CheckInst ("#C", tb2.MakeGenericType (typeof (double)), 0, 16);
115 [Category ("NotDotNet")]
116 // CompilerContext no longer supported
117 [Category ("NotWorking")]
118 public void GetEventMustWorkUnderCompilerContext ()
120 SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
121 var tb = module.DefineType ("foo.type");
122 tb.DefineGenericParameters ("T");
124 var ginst = tb.MakeGenericType (typeof (double));
127 ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
128 } catch (NotSupportedException) {
134 public void MethodsThatRaiseNotSupported ()
136 var tb = module.DefineType ("foo.type");
137 tb.DefineGenericParameters ("T");
139 var ginst = tb.MakeGenericType (typeof (double));
141 /*try { //FIXME this doesn't work yet
142 ginst.GetElementType ();
144 } catch (NotSupportedException) { }*/
146 ginst.GetInterface ("foo", true);
148 } catch (NotSupportedException) { }
150 ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
152 } catch (NotSupportedException) { }
154 ginst.GetField ("foo", BindingFlags.Public | BindingFlags.Instance);
156 } catch (NotSupportedException) { }
158 ginst.GetMembers (BindingFlags.Public | BindingFlags.Instance);
160 } catch (NotSupportedException) { }
162 ginst.GetMethod ("Foo");
164 } catch (NotSupportedException) { }
166 ginst.GetNestedType ("foo", BindingFlags.Public | BindingFlags.Instance);
168 } catch (NotSupportedException) { }
170 ginst.GetProperty ("foo");
172 } catch (NotSupportedException) { }
174 var x = ginst.TypeInitializer;
176 } catch (NotSupportedException) { }
178 var x = ginst.InvokeMember ("foo", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, null, null);
180 } catch (NotSupportedException) { }
182 ginst.IsDefined (typeof (int), true);
184 } catch (NotSupportedException) { }
186 ginst.GetCustomAttributes (true);
188 } catch (NotSupportedException) { }
190 ginst.GetCustomAttributes (typeof (int), true);
192 } catch (NotSupportedException) { }
196 public void ClassMustNotBeRegisteredAfterTypeBuilderIsFinished ()
198 TypeBuilder tb = module.DefineType ("foo.type");
199 tb.DefineGenericParameters ("T");
201 var c = tb.CreateType ();
203 var sreInst = tb.MakeGenericType (typeof (int));
204 var rtInst = c.MakeGenericType (typeof (int));
206 Assert.AreNotSame (sreInst, rtInst, "#1");
208 /*This must not throw*/
209 rtInst.IsDefined (typeof (int), true);