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;
31 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
34 protected void SetUp ()
36 SetUp (AssemblyBuilderAccess.RunAndSave);
39 protected void SetUp (AssemblyBuilderAccess mode)
41 AssemblyName assemblyName = new AssemblyName ();
42 assemblyName.Name = ASSEMBLY_NAME;
45 Thread.GetDomain ().DefineDynamicAssembly (
46 assemblyName, mode, Path.GetTempPath ());
48 module = assembly.DefineDynamicModule ("module1");
53 public void PropertiesValue ()
55 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
56 var gparam = tb.DefineGenericParameters ("A", "B")[1];
58 Assert.AreEqual (assembly, gparam.Assembly, "#1");
59 Assert.AreEqual (null, gparam.AssemblyQualifiedName, "#2");
60 Assert.AreEqual (null, gparam.BaseType, "#3");
61 Assert.AreEqual (null, gparam.FullName, "#4");
62 Assert.AreEqual (module, gparam.Module, "#5");
63 Assert.AreEqual (null, gparam.Namespace, "#6");
64 Assert.AreEqual (gparam, gparam.UnderlyingSystemType, "#7");
65 Assert.AreEqual ("B", gparam.Name, "#8");
68 object x = gparam.GUID;
70 } catch (NotSupportedException) {}
73 object x = gparam.TypeHandle;
75 } catch (NotSupportedException) {}
78 object x = gparam.StructLayoutAttribute;
80 } catch (NotSupportedException) {}
83 Assert.AreEqual (TypeAttributes.Public, gparam.Attributes, "#12");
86 var x = gparam.Attributes;
88 } catch (NotSupportedException) {}
90 Assert.IsFalse (gparam.HasElementType, "#13");
91 Assert.IsFalse (gparam.IsArray, "#14");
92 Assert.IsFalse (gparam.IsByRef, "#15");
93 Assert.IsFalse (gparam.IsCOMObject, "#16");
94 Assert.IsFalse (gparam.IsPointer, "#17");
95 Assert.IsFalse (gparam.IsPrimitive, "#18");
100 public void Methods ()
102 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
103 var gparam = tb.DefineGenericParameters ("A", "B")[1];
106 gparam.GetInterface ("foo", true);
108 } catch (NotSupportedException) {
113 gparam.GetInterfaces ();
115 } catch (NotSupportedException) {
120 gparam.GetElementType ();
122 } catch (NotSupportedException) {
127 gparam.GetEvent ("foo", BindingFlags.Public);
129 } catch (NotSupportedException) {
134 gparam.GetEvents (BindingFlags.Public);
136 } catch (NotSupportedException) {
141 gparam.GetField ("foo", BindingFlags.Public);
143 } catch (NotSupportedException) {
148 gparam.GetFields (BindingFlags.Public);
150 } catch (NotSupportedException) {
155 gparam.GetMembers (BindingFlags.Public);
157 } catch (NotSupportedException) {
162 gparam.GetMethod ("Sort");
164 } catch (NotSupportedException) {
169 gparam.GetMethods (BindingFlags.Public);
171 } catch (NotSupportedException) {
176 gparam.GetNestedType ("bla", BindingFlags.Public);
178 } catch (NotSupportedException) {
183 gparam.GetNestedTypes (BindingFlags.Public);
185 } catch (NotSupportedException) {
190 gparam.GetProperties (BindingFlags.Public);
192 } catch (NotSupportedException) {
197 gparam.GetProperty ("Length");
199 } catch (NotSupportedException) {
204 gparam.GetConstructor (new Type[] { typeof (int) });
206 } catch (NotSupportedException) {
211 gparam.GetArrayRank ();
213 } catch (NotSupportedException) {
218 gparam.GetConstructors (BindingFlags.Public);
220 } catch (NotSupportedException) {}
223 gparam.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
225 } catch (NotSupportedException) {}
228 gparam.IsSubclassOf (gparam);
230 } catch (NotSupportedException) {}
233 gparam.IsAssignableFrom (gparam);
235 } catch (NotSupportedException) {}
238 gparam.GetInterfaceMap (typeof (IEnumerable));
240 } catch (NotSupportedException) {}
243 gparam.IsInstanceOfType (new object());
245 } catch (NotSupportedException) {}
249 public void MakeGenericType ()
251 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
252 var gparam = tb.DefineGenericParameters ("A", "B")[1];
254 gparam.MakeGenericType (new Type[] { typeof (string) });
256 } catch (InvalidOperationException) {}
261 public void GenericParameterAttributes ()
263 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
264 var gparam = tb.DefineGenericParameters ("A", "B")[1];
266 object attr = gparam.GenericParameterAttributes;
268 } catch (NotSupportedException) {}
272 public void MakeArrayType ()
274 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
275 var gparam = tb.DefineGenericParameters ("A", "B")[1];
276 Type res = gparam.MakeArrayType ();
277 Assert.IsNotNull (res, "#1");
278 Assert.IsTrue (res.IsArray, "#2");
280 res = gparam.MakeArrayType (2);
281 Assert.IsNotNull (res, "#3");
282 Assert.IsTrue (res.IsArray, "#4");
286 public void MakeByRefType ()
288 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
289 var gparam = tb.DefineGenericParameters ("A", "B")[1];
290 Type res = gparam.MakeByRefType ();
292 Assert.IsNotNull (res, "#1");
293 Assert.IsTrue (res.IsByRef, "#2");
297 public void MakePointerType ()
299 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
300 var gparam = tb.DefineGenericParameters ("A", "B")[1];
301 Type res = gparam.MakePointerType ();
303 Assert.IsNotNull (res, "#1");
304 Assert.IsTrue (res.IsPointer, "#2");
308 public void SetBaseTypeConstraintWithNull ()
310 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
311 var gparam = tb.DefineGenericParameters ("A", "B")[1];
313 Assert.IsNull (gparam.BaseType, "#1");
314 gparam.SetBaseTypeConstraint (null);
315 Assert.AreEqual (typeof (object), gparam.BaseType, "#2");
319 public void GenericTypeMembers ()
321 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
322 var gparam = tb.DefineGenericParameters ("A", "B")[1];
325 gparam.GetGenericArguments ();
327 } catch (InvalidOperationException) {}
330 gparam.GetGenericParameterConstraints ();
332 } catch (InvalidOperationException) {}
335 gparam.GetGenericTypeDefinition ();
337 } catch (InvalidOperationException) {}
339 Assert.IsTrue (gparam.ContainsGenericParameters, "#4");
341 var x = gparam.GenericParameterAttributes;
343 } catch (NotSupportedException) {}
345 Assert.AreEqual (1, gparam.GenericParameterPosition, "#6");
347 Assert.IsTrue (gparam.ContainsGenericParameters, "#7");
349 Assert.IsTrue (gparam.IsGenericParameter, "#8");
350 Assert.IsFalse (gparam.IsGenericType, "#9");
351 Assert.IsFalse (gparam.IsGenericTypeDefinition, "#10");
355 [Category ("NotDotNet")]
358 // CompilerContext no longer supported
359 [Category ("NotWorking")]
360 public void GetAttributeFlagsImpl ()
362 SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
363 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
364 var gparam = tb.DefineGenericParameters ("A", "B")[1];
366 Assert.AreEqual (TypeAttributes.Public, gparam.Attributes, "#1");