2 // GenericTypeParameterBuilderTest.cs - NUnit Test Cases for GenericTypeParameterBuilder
4 // Rodrigo Kumpera <rkumpera@novell.com>
6 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
12 using System.Collections;
13 using System.Threading;
14 using System.Reflection;
15 using System.Reflection.Emit;
17 using System.Security;
18 using System.Security.Permissions;
19 using System.Runtime.InteropServices;
20 using NUnit.Framework;
21 using System.Runtime.CompilerServices;
23 using System.Collections.Generic;
25 namespace MonoTests.System.Reflection.Emit
28 public class GenericTypeParameterBuilderTest
30 AssemblyBuilder assembly;
33 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
36 protected void SetUp ()
38 SetUp (AssemblyBuilderAccess.RunAndSave);
41 protected void SetUp (AssemblyBuilderAccess mode)
43 AssemblyName assemblyName = new AssemblyName ();
44 assemblyName.Name = ASSEMBLY_NAME;
47 Thread.GetDomain ().DefineDynamicAssembly (
48 assemblyName, mode, Path.GetTempPath ());
50 module = assembly.DefineDynamicModule ("module1");
55 public void PropertiesValue ()
57 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
58 var gparam = tb.DefineGenericParameters ("A", "B")[1];
60 Assert.AreEqual (assembly, gparam.Assembly, "#1");
61 Assert.AreEqual (null, gparam.AssemblyQualifiedName, "#2");
62 Assert.AreEqual (null, gparam.BaseType, "#3");
63 Assert.AreEqual (null, gparam.FullName, "#4");
64 Assert.AreEqual (module, gparam.Module, "#5");
65 Assert.AreEqual (null, gparam.Namespace, "#6");
66 Assert.AreEqual (gparam, gparam.UnderlyingSystemType, "#7");
67 Assert.AreEqual ("B", gparam.Name, "#8");
70 object x = gparam.GUID;
72 } catch (NotSupportedException) {}
75 object x = gparam.TypeHandle;
77 } catch (NotSupportedException) {}
80 object x = gparam.StructLayoutAttribute;
82 } catch (NotSupportedException) {}
85 var x = gparam.Attributes;
87 } catch (NotSupportedException) {}
89 Assert.IsFalse (gparam.HasElementType, "#13");
90 Assert.IsFalse (gparam.IsArray, "#14");
91 Assert.IsFalse (gparam.IsByRef, "#15");
92 Assert.IsFalse (gparam.IsCOMObject, "#16");
93 Assert.IsFalse (gparam.IsPointer, "#17");
94 Assert.IsFalse (gparam.IsPrimitive, "#18");
99 public void Methods ()
101 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
102 var gparam = tb.DefineGenericParameters ("A", "B")[1];
105 gparam.GetInterface ("foo", true);
107 } catch (NotSupportedException) {
112 gparam.GetInterfaces ();
114 } catch (NotSupportedException) {
119 gparam.GetElementType ();
121 } catch (NotSupportedException) {
126 gparam.GetEvent ("foo", BindingFlags.Public);
128 } catch (NotSupportedException) {
133 gparam.GetEvents (BindingFlags.Public);
135 } catch (NotSupportedException) {
140 gparam.GetField ("foo", BindingFlags.Public);
142 } catch (NotSupportedException) {
147 gparam.GetFields (BindingFlags.Public);
149 } catch (NotSupportedException) {
154 gparam.GetMembers (BindingFlags.Public);
156 } catch (NotSupportedException) {
161 gparam.GetMethod ("Sort");
163 } catch (NotSupportedException) {
168 gparam.GetMethods (BindingFlags.Public);
170 } catch (NotSupportedException) {
175 gparam.GetNestedType ("bla", BindingFlags.Public);
177 } catch (NotSupportedException) {
182 gparam.GetNestedTypes (BindingFlags.Public);
184 } catch (NotSupportedException) {
189 gparam.GetProperties (BindingFlags.Public);
191 } catch (NotSupportedException) {
196 gparam.GetProperty ("Length");
198 } catch (NotSupportedException) {
203 gparam.GetConstructor (new Type[] { typeof (int) });
205 } catch (NotSupportedException) {
210 gparam.GetArrayRank ();
212 } catch (NotSupportedException) {
217 gparam.GetConstructors (BindingFlags.Public);
219 } catch (NotSupportedException) {}
222 gparam.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
224 } catch (NotSupportedException) {}
227 gparam.IsSubclassOf (gparam);
229 } catch (NotSupportedException) {}
232 gparam.IsAssignableFrom (gparam);
234 } catch (NotSupportedException) {}
237 gparam.GetInterfaceMap (typeof (IEnumerable));
239 } catch (NotSupportedException) {}
242 gparam.IsInstanceOfType (new object());
244 } catch (NotSupportedException) {}
248 public void MakeGenericType ()
250 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
251 var gparam = tb.DefineGenericParameters ("A", "B")[1];
253 gparam.MakeGenericType (new Type[] { typeof (string) });
255 } catch (InvalidOperationException) {}
260 public void GenericParameterAttributes ()
262 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
263 var gparam = tb.DefineGenericParameters ("A", "B")[1];
265 object attr = gparam.GenericParameterAttributes;
267 } catch (NotSupportedException) {}
271 public void MakeArrayType ()
273 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
274 var gparam = tb.DefineGenericParameters ("A", "B")[1];
275 Type res = gparam.MakeArrayType ();
276 Assert.IsNotNull (res, "#1");
277 Assert.IsTrue (res.IsArray, "#2");
279 res = gparam.MakeArrayType (2);
280 Assert.IsNotNull (res, "#3");
281 Assert.IsTrue (res.IsArray, "#4");
285 public void MakeByRefType ()
287 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
288 var gparam = tb.DefineGenericParameters ("A", "B")[1];
289 Type res = gparam.MakeByRefType ();
291 Assert.IsNotNull (res, "#1");
292 Assert.IsTrue (res.IsByRef, "#2");
296 public void MakePointerType ()
298 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
299 var gparam = tb.DefineGenericParameters ("A", "B")[1];
300 Type res = gparam.MakePointerType ();
302 Assert.IsNotNull (res, "#1");
303 Assert.IsTrue (res.IsPointer, "#2");
308 public void GenericTypeMembers ()
310 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
311 var gparam = tb.DefineGenericParameters ("A", "B")[1];
314 gparam.GetGenericArguments ();
316 } catch (InvalidOperationException) {}
319 gparam.GetGenericParameterConstraints ();
321 } catch (InvalidOperationException) {}
324 gparam.GetGenericTypeDefinition ();
326 } catch (InvalidOperationException) {}
328 Assert.IsTrue (gparam.ContainsGenericParameters, "#4");
330 var x = gparam.GenericParameterAttributes;
332 } catch (NotSupportedException) {}
334 Assert.AreEqual (1, gparam.GenericParameterPosition, "#6");
336 Assert.IsTrue (gparam.ContainsGenericParameters, "#7");
338 Assert.IsTrue (gparam.IsGenericParameter, "#8");
339 Assert.IsFalse (gparam.IsGenericType, "#9");
340 Assert.IsFalse (gparam.IsGenericTypeDefinition, "#10");
344 [Category ("NotDotNet")]
345 public void GetAttributeFlagsImplWorksUnderCompilerContext ()
347 SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
348 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
349 var gparam = tb.DefineGenericParameters ("A", "B")[1];
351 Assert.AreEqual (TypeAttributes.Public, gparam.Attributes, "#1");