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
27 public class MonoGenericClassTest
29 AssemblyBuilder assembly;
32 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.MonoGenericClassTest";
36 return "internal__type"+ typeCount++;
42 SetUp (AssemblyBuilderAccess.RunAndSave);
45 void SetUp (AssemblyBuilderAccess access)
47 AssemblyName assemblyName = new AssemblyName ();
48 assemblyName.Name = ASSEMBLY_NAME;
51 Thread.GetDomain ().DefineDynamicAssembly (
52 assemblyName, access, Path.GetTempPath ());
54 module = assembly.DefineDynamicModule ("module1");
60 public void TestNameMethods ()
62 TypeBuilder tb = module.DefineType ("foo.type");
63 tb.DefineGenericParameters ("T", "K");
65 Type inst = tb.MakeGenericType (typeof (double), typeof (string));
67 Assert.AreEqual ("type", inst.Name, "#1");
68 Assert.AreEqual ("foo", inst.Namespace, "#2");
70 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");
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]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
73 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");
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]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
76 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");
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]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
79 Assert.AreEqual ("foo.type[System.Double,System.String]", inst.ToString (), "#5");
82 static void CheckInst (string prefix, Type inst, int a, int b)
84 var resA = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
85 var resB = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance);
87 Assert.AreEqual (a, resA.Length, prefix + 1);
88 Assert.AreEqual (b, resB.Length, prefix + 2);
92 public void MethodsThatRaiseNotSupported ()
94 var tb = module.DefineType ("foo.type");
95 tb.DefineGenericParameters ("T");
97 var ginst = tb.MakeGenericType (typeof (double));
100 ginst.GetElementType ();
102 } catch (NotSupportedException) { }
104 ginst.GetInterface ("foo", true);
106 } catch (NotSupportedException) { }
108 ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
110 } catch (NotSupportedException) { }
112 ginst.GetField ("foo", BindingFlags.Public | BindingFlags.Instance);
114 } catch (NotSupportedException) { }
116 ginst.GetMembers (BindingFlags.Public | BindingFlags.Instance);
118 } catch (NotSupportedException) { }
120 ginst.GetMethod ("Foo");
122 } catch (NotSupportedException) { }
124 ginst.GetNestedType ("foo", BindingFlags.Public | BindingFlags.Instance);
126 } catch (NotSupportedException) { }
128 ginst.GetProperty ("foo");
130 } catch (NotSupportedException) { }
132 var x = ginst.TypeInitializer;
134 } catch (NotSupportedException) { }
136 var x = ginst.InvokeMember ("foo", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, null, null);
138 } catch (NotSupportedException) { }
140 ginst.IsDefined (typeof (int), true);
142 } catch (NotSupportedException) { }
144 ginst.GetCustomAttributes (true);
146 } catch (NotSupportedException) { }
148 ginst.GetCustomAttributes (typeof (int), true);
150 } catch (NotSupportedException) { }
152 ginst.IsAssignableFrom (ginst);
154 } catch (NotSupportedException) { }
156 ginst.GetNestedTypes (BindingFlags.Public);
158 } catch (NotSupportedException) { }
162 public void ClassMustNotBeRegisteredAfterTypeBuilderIsFinished ()
164 TypeBuilder tb = module.DefineType ("foo.type");
165 tb.DefineGenericParameters ("T");
167 var c = tb.CreateType ();
169 var sreInst = tb.MakeGenericType (typeof (int));
170 var rtInst = c.MakeGenericType (typeof (int));
172 Assert.AreNotSame (sreInst, rtInst, "#1");
174 /*This must not throw*/
175 rtInst.IsDefined (typeof (int), true);
178 public class Bar<T> {
179 public class Foo<T> {}
183 public void DeclaringTypeMustReturnNonInflatedType ()
185 var ut = new TypeDelegator (typeof (int));
186 var ut2 = typeof(Bar<>.Foo<>);
187 var t = ut2.MakeGenericType (ut, ut);
188 Assert.AreSame (typeof (Bar<>), t.DeclaringType, "#1");
191 public class Base<T> {}
192 public class SubClass<K> : Base<K> {}
195 public void BaseTypeMustReturnNonInflatedType ()
197 var ut = new TypeDelegator (typeof (int));
198 var ut2 = typeof(SubClass<>);
199 var t = ut2.MakeGenericType (ut);
200 //This is Base<K> where K is SubClass::K
201 var expected = typeof (Base<>).MakeGenericType (typeof (SubClass<>).GetGenericArguments ()[0]);
202 Assert.AreSame (expected, t.BaseType, "#1");