2 // GenericTypeParameterBuilderTest.cs - NUnit Test Cases for GenericTypeParameterBuilder
4 // Rodrigo Kumpera <rkumpera@novell.com>
6 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
10 using System.Collections;
11 using System.Threading;
12 using System.Reflection;
13 using System.Reflection.Emit;
15 using System.Security;
16 using System.Security.Permissions;
17 using System.Runtime.InteropServices;
18 using NUnit.Framework;
19 using System.Runtime.CompilerServices;
21 using System.Collections.Generic;
23 namespace MonoTests.System.Reflection.Emit
26 public class GenericTypeParameterBuilderTest
28 AssemblyBuilder assembly;
30 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
33 protected void SetUp ()
35 SetUp (AssemblyBuilderAccess.RunAndSave);
38 protected void SetUp (AssemblyBuilderAccess mode)
40 AssemblyName assemblyName = new AssemblyName ();
41 assemblyName.Name = ASSEMBLY_NAME;
44 Thread.GetDomain ().DefineDynamicAssembly (
45 assemblyName, mode, Path.GetTempPath ());
47 module = assembly.DefineDynamicModule ("module1");
51 public void PropertiesValue ()
53 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
54 var gparam = tb.DefineGenericParameters ("A", "B")[1];
56 Assert.AreEqual (assembly, gparam.Assembly, "#1");
57 Assert.AreEqual (null, gparam.AssemblyQualifiedName, "#2");
58 Assert.AreEqual (null, gparam.BaseType, "#3");
59 Assert.AreEqual (null, gparam.FullName, "#4");
60 Assert.AreEqual (module, gparam.Module, "#5");
61 Assert.AreEqual (null, gparam.Namespace, "#6");
62 Assert.AreEqual (gparam, gparam.UnderlyingSystemType, "#7");
63 Assert.AreEqual ("B", gparam.Name, "#8");
66 object x = gparam.GUID;
68 } catch (NotSupportedException) {}
71 object x = gparam.TypeHandle;
73 } catch (NotSupportedException) {}
76 object x = gparam.StructLayoutAttribute;
78 } catch (NotSupportedException) {}
81 Assert.AreEqual (TypeAttributes.Public, gparam.Attributes, "#12");
84 var x = gparam.Attributes;
86 } catch (NotSupportedException) {}
88 Assert.IsFalse (gparam.HasElementType, "#13");
89 Assert.IsFalse (gparam.IsArray, "#14");
90 Assert.IsFalse (gparam.IsByRef, "#15");
91 Assert.IsFalse (gparam.IsCOMObject, "#16");
92 Assert.IsFalse (gparam.IsPointer, "#17");
93 Assert.IsFalse (gparam.IsPrimitive, "#18");
98 public void Methods ()
100 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
101 var gparam = tb.DefineGenericParameters ("A", "B")[1];
104 gparam.GetInterface ("foo", true);
106 } catch (NotSupportedException) {
111 gparam.GetInterfaces ();
113 } catch (NotSupportedException) {
118 gparam.GetElementType ();
120 } catch (NotSupportedException) {
125 gparam.GetEvent ("foo", BindingFlags.Public);
127 } catch (NotSupportedException) {
132 gparam.GetEvents (BindingFlags.Public);
134 } catch (NotSupportedException) {
139 gparam.GetField ("foo", BindingFlags.Public);
141 } catch (NotSupportedException) {
146 gparam.GetFields (BindingFlags.Public);
148 } catch (NotSupportedException) {
153 gparam.GetMembers (BindingFlags.Public);
155 } catch (NotSupportedException) {
160 gparam.GetMethod ("Sort");
162 } catch (NotSupportedException) {
167 gparam.GetMethods (BindingFlags.Public);
169 } catch (NotSupportedException) {
174 gparam.GetNestedType ("bla", BindingFlags.Public);
176 } catch (NotSupportedException) {
181 gparam.GetNestedTypes (BindingFlags.Public);
183 } catch (NotSupportedException) {
188 gparam.GetProperties (BindingFlags.Public);
190 } catch (NotSupportedException) {
195 gparam.GetProperty ("Length");
197 } catch (NotSupportedException) {
202 gparam.GetConstructor (new Type[] { typeof (int) });
204 } catch (NotSupportedException) {
209 gparam.GetArrayRank ();
211 } catch (NotSupportedException) {
216 gparam.GetConstructors (BindingFlags.Public);
218 } catch (NotSupportedException) {}
221 gparam.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
223 } catch (NotSupportedException) {}
226 gparam.IsSubclassOf (gparam);
228 } catch (NotSupportedException) {}
231 gparam.IsAssignableFrom (gparam);
233 } catch (NotSupportedException) {}
236 gparam.GetInterfaceMap (typeof (IEnumerable));
238 } catch (NotSupportedException) {}
241 gparam.IsInstanceOfType (new object());
243 } catch (NotSupportedException) {}
247 public void MakeGenericType ()
249 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
250 var gparam = tb.DefineGenericParameters ("A", "B")[1];
252 gparam.MakeGenericType (new Type[] { typeof (string) });
254 } catch (InvalidOperationException) {}
259 public void GenericParameterAttributes ()
261 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
262 var gparam = tb.DefineGenericParameters ("A", "B")[1];
264 object attr = gparam.GenericParameterAttributes;
266 } catch (NotSupportedException) {}
270 public void MakeArrayType ()
272 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
273 var gparam = tb.DefineGenericParameters ("A", "B")[1];
274 Type res = gparam.MakeArrayType ();
275 Assert.IsNotNull (res, "#1");
276 Assert.IsTrue (res.IsArray, "#2");
278 res = gparam.MakeArrayType (2);
279 Assert.IsNotNull (res, "#3");
280 Assert.IsTrue (res.IsArray, "#4");
284 public void MakeByRefType ()
286 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
287 var gparam = tb.DefineGenericParameters ("A", "B")[1];
288 Type res = gparam.MakeByRefType ();
290 Assert.IsNotNull (res, "#1");
291 Assert.IsTrue (res.IsByRef, "#2");
295 public void MakePointerType ()
297 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
298 var gparam = tb.DefineGenericParameters ("A", "B")[1];
299 Type res = gparam.MakePointerType ();
301 Assert.IsNotNull (res, "#1");
302 Assert.IsTrue (res.IsPointer, "#2");
306 public void SetBaseTypeConstraintWithNull ()
308 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
309 var gparam = tb.DefineGenericParameters ("A", "B")[1];
311 Assert.IsNull (gparam.BaseType, "#1");
312 gparam.SetBaseTypeConstraint (null);
313 Assert.AreEqual (typeof (object), gparam.BaseType, "#2");
317 public void GenericTypeMembers ()
319 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
320 var gparam = tb.DefineGenericParameters ("A", "B")[1];
323 gparam.GetGenericArguments ();
325 } catch (InvalidOperationException) {}
328 gparam.GetGenericParameterConstraints ();
330 } catch (InvalidOperationException) {}
333 gparam.GetGenericTypeDefinition ();
335 } catch (InvalidOperationException) {}
337 Assert.IsTrue (gparam.ContainsGenericParameters, "#4");
339 var x = gparam.GenericParameterAttributes;
341 } catch (NotSupportedException) {}
343 Assert.AreEqual (1, gparam.GenericParameterPosition, "#6");
345 Assert.IsTrue (gparam.ContainsGenericParameters, "#7");
347 Assert.IsTrue (gparam.IsGenericParameter, "#8");
348 Assert.IsFalse (gparam.IsGenericType, "#9");
349 Assert.IsFalse (gparam.IsGenericTypeDefinition, "#10");
353 [Category ("NotDotNet")]
356 // CompilerContext no longer supported
357 [Category ("NotWorking")]
358 public void GetAttributeFlagsImpl ()
360 SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
361 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
362 var gparam = tb.DefineGenericParameters ("A", "B")[1];
364 Assert.AreEqual (TypeAttributes.Public, gparam.Attributes, "#1");