MonoGenericClassTest.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // MonoGenericClassTest.cs - NUnit Test Cases for MonoGenericClassTest
  3. //
  4. // Rodrigo Kumpera <[email protected]>
  5. //
  6. // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
  7. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  8. //
  9. #if !MONOTOUCH
  10. using System;
  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;
  17. using System.IO;
  18. using System.Security;
  19. using System.Security.Permissions;
  20. using System.Runtime.InteropServices;
  21. using NUnit.Framework;
  22. using System.Runtime.CompilerServices;
  23. namespace MonoTests.System.Reflection.Emit
  24. {
  25. [TestFixture]
  26. public class MonoGenericClassTest
  27. {
  28. AssemblyBuilder assembly;
  29. ModuleBuilder module;
  30. int typeCount;
  31. static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.MonoGenericClassTest";
  32. string MakeName ()
  33. {
  34. return "internal__type"+ typeCount++;
  35. }
  36. [SetUp]
  37. public void SetUp ()
  38. {
  39. SetUp (AssemblyBuilderAccess.RunAndSave);
  40. }
  41. void SetUp (AssemblyBuilderAccess access)
  42. {
  43. AssemblyName assemblyName = new AssemblyName ();
  44. assemblyName.Name = ASSEMBLY_NAME;
  45. assembly =
  46. Thread.GetDomain ().DefineDynamicAssembly (
  47. assemblyName, access, Path.GetTempPath ());
  48. module = assembly.DefineDynamicModule ("module1");
  49. typeCount = 0;
  50. }
  51. [Test]
  52. public void TestNameMethods ()
  53. {
  54. TypeBuilder tb = module.DefineType ("foo.type");
  55. tb.DefineGenericParameters ("T", "K");
  56. Type inst = tb.MakeGenericType (typeof (double), typeof (string));
  57. Assert.AreEqual ("type", inst.Name, "#1");
  58. Assert.AreEqual ("foo", inst.Namespace, "#2");
  59. #if NET_4_0
  60. 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");
  61. 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");
  62. #elif NET_2_1
  63. 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");
  64. 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");
  65. #else
  66. 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");
  67. 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");
  68. #endif
  69. Assert.AreEqual ("foo.type[System.Double,System.String]", inst.ToString (), "#5");
  70. }
  71. static void CheckInst (string prefix, Type inst, int a, int b)
  72. {
  73. var resA = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
  74. var resB = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance);
  75. Assert.AreEqual (a, resA.Length, prefix + 1);
  76. Assert.AreEqual (b, resB.Length, prefix + 2);
  77. }
  78. [Test]
  79. public void MethodsThatRaiseNotSupported ()
  80. {
  81. var tb = module.DefineType ("foo.type");
  82. tb.DefineGenericParameters ("T");
  83. var ginst = tb.MakeGenericType (typeof (double));
  84. try {
  85. ginst.GetElementType ();
  86. Assert.Fail ("#1");
  87. } catch (NotSupportedException) { }
  88. try {
  89. ginst.GetInterface ("foo", true);
  90. Assert.Fail ("#2");
  91. } catch (NotSupportedException) { }
  92. try {
  93. ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
  94. Assert.Fail ("#3");
  95. } catch (NotSupportedException) { }
  96. try {
  97. ginst.GetField ("foo", BindingFlags.Public | BindingFlags.Instance);
  98. Assert.Fail ("#4");
  99. } catch (NotSupportedException) { }
  100. try {
  101. ginst.GetMembers (BindingFlags.Public | BindingFlags.Instance);
  102. Assert.Fail ("#5");
  103. } catch (NotSupportedException) { }
  104. try {
  105. ginst.GetMethod ("Foo");
  106. Assert.Fail ("#6");
  107. } catch (NotSupportedException) { }
  108. try {
  109. ginst.GetNestedType ("foo", BindingFlags.Public | BindingFlags.Instance);
  110. Assert.Fail ("#7");
  111. } catch (NotSupportedException) { }
  112. try {
  113. ginst.GetProperty ("foo");
  114. Assert.Fail ("#8");
  115. } catch (NotSupportedException) { }
  116. try {
  117. var x = ginst.TypeInitializer;
  118. Assert.Fail ("#9");
  119. } catch (NotSupportedException) { }
  120. try {
  121. var x = ginst.InvokeMember ("foo", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, null, null);
  122. Assert.Fail ("#10");
  123. } catch (NotSupportedException) { }
  124. try {
  125. ginst.IsDefined (typeof (int), true);
  126. Assert.Fail ("#11");
  127. } catch (NotSupportedException) { }
  128. try {
  129. ginst.GetCustomAttributes (true);
  130. Assert.Fail ("#12");
  131. } catch (NotSupportedException) { }
  132. try {
  133. ginst.GetCustomAttributes (typeof (int), true);
  134. Assert.Fail ("#13");
  135. } catch (NotSupportedException) { }
  136. try {
  137. ginst.IsAssignableFrom (ginst);
  138. Assert.Fail ("#14");
  139. } catch (NotSupportedException) { }
  140. try {
  141. ginst.GetNestedTypes (BindingFlags.Public);
  142. Assert.Fail ("#14");
  143. } catch (NotSupportedException) { }
  144. }
  145. [Test]
  146. public void ClassMustNotBeRegisteredAfterTypeBuilderIsFinished ()
  147. {
  148. TypeBuilder tb = module.DefineType ("foo.type");
  149. tb.DefineGenericParameters ("T");
  150. var c = tb.CreateType ();
  151. var sreInst = tb.MakeGenericType (typeof (int));
  152. var rtInst = c.MakeGenericType (typeof (int));
  153. Assert.AreNotSame (sreInst, rtInst, "#1");
  154. /*This must not throw*/
  155. rtInst.IsDefined (typeof (int), true);
  156. }
  157. public class Bar<T> {
  158. public class Foo<T> {}
  159. }
  160. [Test]
  161. public void DeclaringTypeMustReturnNonInflatedType ()
  162. {
  163. var ut = new TypeDelegator (typeof (int));
  164. var ut2 = typeof(Bar<>.Foo<>);
  165. var t = ut2.MakeGenericType (ut, ut);
  166. Assert.AreSame (typeof (Bar<>), t.DeclaringType, "#1");
  167. }
  168. public class Base<T> {}
  169. public class SubClass<K> : Base<K> {}
  170. [Test]
  171. public void BaseTypeMustReturnNonInflatedType ()
  172. {
  173. var ut = new TypeDelegator (typeof (int));
  174. var ut2 = typeof(SubClass<>);
  175. var t = ut2.MakeGenericType (ut);
  176. //This is Base<K> where K is SubClass::K
  177. var expected = typeof (Base<>).MakeGenericType (typeof (SubClass<>).GetGenericArguments ()[0]);
  178. Assert.AreSame (expected, t.BaseType, "#1");
  179. }
  180. }
  181. }
  182. #endif