2 // MonoGenericClassTest.cs - NUnit Test Cases for MonoGenericClassTest
4 // Rodrigo Kumpera <rkumpera@novell.com>
6 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Collections.ObjectModel;
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;
24 using System.Collections.Generic;
27 namespace MonoTests.System.Reflection.Emit
31 public class MonoGenericClassTest
33 AssemblyBuilder assembly;
36 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.MonoGenericClassTest";
40 return "internal__type"+ typeCount++;
46 SetUp (AssemblyBuilderAccess.RunAndSave);
49 void SetUp (AssemblyBuilderAccess access)
51 AssemblyName assemblyName = new AssemblyName ();
52 assemblyName.Name = ASSEMBLY_NAME;
55 Thread.GetDomain ().DefineDynamicAssembly (
56 assemblyName, access, Path.GetTempPath ());
58 module = assembly.DefineDynamicModule ("module1");
64 public void TestNameMethods ()
66 TypeBuilder tb = module.DefineType ("foo.type");
67 tb.DefineGenericParameters ("T", "K");
69 Type inst = tb.MakeGenericType (typeof (double), typeof (string));
71 Assert.AreEqual ("type", inst.Name, "#1");
72 Assert.AreEqual ("foo", inst.Namespace, "#2");
74 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");
75 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");
79 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");
80 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");
83 Assert.AreEqual ("foo.type[System.Double,System.String]", inst.ToString (), "#5");
86 static void CheckInst (string prefix, Type inst, int a, int b)
88 var resA = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
89 var resB = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance);
91 Assert.AreEqual (a, resA.Length, prefix + 1);
92 Assert.AreEqual (b, resB.Length, prefix + 2);
96 [Category ("NotDotNet")]
97 public void GetMethodsWorkWithFunkyInstantiations ()
99 SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
100 TypeBuilder tb = module.DefineType ("Base", TypeAttributes.Public, typeof (object));
102 var a = typeof (IList<>).GetGenericArguments () [0];
103 var b = tb.DefineGenericParameters ("T") [0];
105 CheckInst ("#A", typeof (Collection<>).MakeGenericType (new Type [] {a}), 12, 16);
106 CheckInst ("#B", typeof (Collection<>).MakeGenericType (new Type[] { b }), 12, 16);
108 var tb2 = module.DefineType ("Child", TypeAttributes.Public, typeof (Collection<>).MakeGenericType (tb.MakeGenericType (typeof (int))));
109 tb2.DefineGenericParameters ("K");
111 CheckInst ("#C", tb2.MakeGenericType (typeof (double)), 0, 16);
116 [Category ("NotDotNet")]
117 public void GetEventMustWorkUnderCompilerContext ()
119 SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
120 var tb = module.DefineType ("foo.type");
121 tb.DefineGenericParameters ("T");
123 var ginst = tb.MakeGenericType (typeof (double));
126 ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
127 } catch (NotSupportedException) {
133 public void MethodsThatRaiseNotSupported ()
135 var tb = module.DefineType ("foo.type");
136 tb.DefineGenericParameters ("T");
138 var ginst = tb.MakeGenericType (typeof (double));
140 /*try { //FIXME this doesn't work yet
141 ginst.GetElementType ();
143 } catch (NotSupportedException) { }*/
145 ginst.GetInterface ("foo", true);
147 } catch (NotSupportedException) { }
149 ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
151 } catch (NotSupportedException) { }
153 ginst.GetField ("foo", BindingFlags.Public | BindingFlags.Instance);
155 } catch (NotSupportedException) { }
157 ginst.GetMembers (BindingFlags.Public | BindingFlags.Instance);
159 } catch (NotSupportedException) { }
161 ginst.GetMethod ("Foo");
163 } catch (NotSupportedException) { }
165 ginst.GetNestedType ("foo", BindingFlags.Public | BindingFlags.Instance);
167 } catch (NotSupportedException) { }
169 ginst.GetProperty ("foo");
171 } catch (NotSupportedException) { }
173 var x = ginst.TypeInitializer;
175 } catch (NotSupportedException) { }
177 var x = ginst.InvokeMember ("foo", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, null, null);
179 } catch (NotSupportedException) { }
181 ginst.IsDefined (typeof (int), true);
183 } catch (NotSupportedException) { }
185 ginst.GetCustomAttributes (true);
187 } catch (NotSupportedException) { }
189 ginst.GetCustomAttributes (typeof (int), true);
191 } catch (NotSupportedException) { }