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).
13 using System.Collections;
14 using System.Collections.Generic;
15 using System.Collections.ObjectModel;
16 using System.Threading;
17 using System.Reflection;
18 using System.Reflection.Emit;
20 using System.Security;
21 using System.Security.Permissions;
22 using System.Runtime.InteropServices;
23 using NUnit.Framework;
24 using System.Runtime.CompilerServices;
26 namespace MonoTests.System.Reflection.Emit
29 public class MonoGenericClassTest
31 AssemblyBuilder assembly;
34 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.MonoGenericClassTest";
38 return "internal__type"+ typeCount++;
44 SetUp (AssemblyBuilderAccess.RunAndSave);
47 void SetUp (AssemblyBuilderAccess access)
49 AssemblyName assemblyName = new AssemblyName ();
50 assemblyName.Name = ASSEMBLY_NAME;
53 Thread.GetDomain ().DefineDynamicAssembly (
54 assemblyName, access, Path.GetTempPath ());
56 module = assembly.DefineDynamicModule ("module1");
62 public void TestNameMethods ()
64 TypeBuilder tb = module.DefineType ("foo.type");
65 tb.DefineGenericParameters ("T", "K");
67 Type inst = tb.MakeGenericType (typeof (double), typeof (string));
69 Assert.AreEqual ("type", inst.Name, "#1");
70 Assert.AreEqual ("foo", inst.Namespace, "#2");
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]]", inst.FullName, "#3");
73 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");
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]]", inst.FullName, "#3");
76 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");
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]]", inst.FullName, "#3");
79 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");
81 Assert.AreEqual ("foo.type[System.Double,System.String]", inst.ToString (), "#5");
84 static void CheckInst (string prefix, Type inst, int a, int b)
86 var resA = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
87 var resB = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance);
89 Assert.AreEqual (a, resA.Length, prefix + 1);
90 Assert.AreEqual (b, resB.Length, prefix + 2);
94 public void MethodsThatRaiseNotSupported ()
96 var tb = module.DefineType ("foo.type");
97 tb.DefineGenericParameters ("T");
99 var ginst = tb.MakeGenericType (typeof (double));
102 ginst.GetElementType ();
104 } catch (NotSupportedException) { }
106 ginst.GetInterface ("foo", true);
108 } catch (NotSupportedException) { }
110 ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
112 } catch (NotSupportedException) { }
114 ginst.GetField ("foo", BindingFlags.Public | BindingFlags.Instance);
116 } catch (NotSupportedException) { }
118 ginst.GetMembers (BindingFlags.Public | BindingFlags.Instance);
120 } catch (NotSupportedException) { }
122 ginst.GetMethod ("Foo");
124 } catch (NotSupportedException) { }
126 ginst.GetNestedType ("foo", BindingFlags.Public | BindingFlags.Instance);
128 } catch (NotSupportedException) { }
130 ginst.GetProperty ("foo");
132 } catch (NotSupportedException) { }
134 var x = ginst.TypeInitializer;
136 } catch (NotSupportedException) { }
138 var x = ginst.InvokeMember ("foo", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, null, null);
140 } catch (NotSupportedException) { }
142 ginst.IsDefined (typeof (int), true);
144 } catch (NotSupportedException) { }
146 ginst.GetCustomAttributes (true);
148 } catch (NotSupportedException) { }
150 ginst.GetCustomAttributes (typeof (int), true);
152 } catch (NotSupportedException) { }
154 ginst.IsAssignableFrom (ginst);
156 } catch (NotSupportedException) { }
158 ginst.GetNestedTypes (BindingFlags.Public);
160 } catch (NotSupportedException) { }
164 public void ClassMustNotBeRegisteredAfterTypeBuilderIsFinished ()
166 TypeBuilder tb = module.DefineType ("foo.type");
167 tb.DefineGenericParameters ("T");
169 var c = tb.CreateType ();
171 var sreInst = tb.MakeGenericType (typeof (int));
172 var rtInst = c.MakeGenericType (typeof (int));
174 Assert.AreNotSame (sreInst, rtInst, "#1");
176 /*This must not throw*/
177 rtInst.IsDefined (typeof (int), true);
180 public class Bar<T> {
181 public class Foo<U> {}
185 public void DeclaringTypeMustReturnNonInflatedType ()
187 var ut = new TypeDelegator (typeof (int));
188 var ut2 = typeof(Bar<>.Foo<>);
189 var t = ut2.MakeGenericType (ut, ut);
190 Assert.AreSame (typeof (Bar<>), t.DeclaringType, "#1");
193 public class Base<T> {}
194 public class SubClass<K> : Base<K> {}
197 public void BaseTypeMustReturnNonInflatedType ()
199 var ut = new TypeDelegator (typeof (int));
200 var ut2 = typeof(SubClass<>);
201 var t = ut2.MakeGenericType (ut);
202 //This is Base<K> where K is SubClass::K
203 var expected = typeof (Base<>).MakeGenericType (typeof (SubClass<>).GetGenericArguments ()[0]);
204 Assert.AreSame (expected, t.BaseType, "#1");