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
;
23 namespace MonoTests
.System
.Reflection
.Emit
27 public class MonoGenericClassTest
29 AssemblyBuilder assembly
;
32 static string ASSEMBLY_NAME
= "MonoTests.System.Reflection.Emit.MonoGenericClassTest";
36 return "internal__type"+ typeCount
++;
42 SetUp (AssemblyBuilderAccess
.RunAndSave
);
45 void SetUp (AssemblyBuilderAccess access
)
47 AssemblyName assemblyName
= new AssemblyName ();
48 assemblyName
.Name
= ASSEMBLY_NAME
;
51 Thread
.GetDomain ().DefineDynamicAssembly (
52 assemblyName
, access
, Path
.GetTempPath ());
54 module
= assembly
.DefineDynamicModule ("module1");
60 public void TestNameMethods ()
62 TypeBuilder tb
= module
.DefineType ("foo.type");
63 tb
.DefineGenericParameters ("T", "K");
65 Type inst
= tb
.MakeGenericType (typeof (double), typeof (string));
67 Assert
.AreEqual ("type", inst
.Name
, "#1");
68 Assert
.AreEqual ("foo", inst
.Namespace
, "#2");
70 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");
71 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");
75 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");
76 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");
79 Assert
.AreEqual ("foo.type[System.Double,System.String]", inst
.ToString (), "#5");
82 static void CheckInst (string prefix
, Type inst
, int a
, int b
)
84 var resA
= inst
.GetMethods (BindingFlags
.Public
| BindingFlags
.Instance
| BindingFlags
.DeclaredOnly
);
85 var resB
= inst
.GetMethods (BindingFlags
.Public
| BindingFlags
.Instance
);
87 Assert
.AreEqual (a
, resA
.Length
, prefix
+ 1);
88 Assert
.AreEqual (b
, resB
.Length
, prefix
+ 2);
92 [Category ("NotDotNet")]
93 public void GetMethodsWorkWithFunkyInstantiations ()
95 SetUp (AssemblyBuilderAccess
.RunAndSave
| (AssemblyBuilderAccess
)0x800);
96 TypeBuilder tb
= module
.DefineType ("Base", TypeAttributes
.Public
, typeof (object));
98 var a
= typeof (IList
<>).GetGenericArguments () [0];
99 var b
= tb
.DefineGenericParameters ("T") [0];
101 CheckInst ("#A", typeof (Collection
<>).MakeGenericType (new Type
[] {a}
), 12, 16);
102 CheckInst ("#B", typeof (Collection
<>).MakeGenericType (new Type
[] { b }
), 12, 16);
104 var tb2
= module
.DefineType ("Child", TypeAttributes
.Public
, typeof (Collection
<>).MakeGenericType (tb
.MakeGenericType (typeof (int))));
105 tb2
.DefineGenericParameters ("K");
107 CheckInst ("#C", tb2
.MakeGenericType (typeof (double)), 0, 16);
112 [Category ("NotDotNet")]
113 public void GetEventMustWorkUnderCompilerContext ()
115 SetUp (AssemblyBuilderAccess
.RunAndSave
| (AssemblyBuilderAccess
)0x800);
116 var tb
= module
.DefineType ("foo.type");
117 tb
.DefineGenericParameters ("T");
119 var ginst
= tb
.MakeGenericType (typeof (double));
122 ginst
.GetEvent ("foo", BindingFlags
.Public
| BindingFlags
.Instance
);
123 } catch (NotSupportedException
) {
129 public void MethodsThatRaiseNotSupported ()
131 var tb
= module
.DefineType ("foo.type");
132 tb
.DefineGenericParameters ("T");
134 var ginst
= tb
.MakeGenericType (typeof (double));
136 /*try { //FIXME this doesn't work yet
137 ginst.GetElementType ();
139 } catch (NotSupportedException) { }*/
141 ginst
.GetInterface ("foo", true);
143 } catch (NotSupportedException
) { }
145 ginst
.GetEvent ("foo", BindingFlags
.Public
| BindingFlags
.Instance
);
147 } catch (NotSupportedException
) { }
149 ginst
.GetField ("foo", BindingFlags
.Public
| BindingFlags
.Instance
);
151 } catch (NotSupportedException
) { }
153 ginst
.GetMembers (BindingFlags
.Public
| BindingFlags
.Instance
);
155 } catch (NotSupportedException
) { }
157 ginst
.GetMethod ("Foo");
159 } catch (NotSupportedException
) { }
161 ginst
.GetNestedType ("foo", BindingFlags
.Public
| BindingFlags
.Instance
);
163 } catch (NotSupportedException
) { }
165 ginst
.GetProperty ("foo");
167 } catch (NotSupportedException
) { }
169 var x
= ginst
.TypeInitializer
;
171 } catch (NotSupportedException
) { }
173 var x
= ginst
.InvokeMember ("foo", BindingFlags
.InvokeMethod
| BindingFlags
.Public
| BindingFlags
.Instance
, null, null, null);
175 } catch (NotSupportedException
) { }
177 ginst
.IsDefined (typeof (int), true);
179 } catch (NotSupportedException
) { }
181 ginst
.GetCustomAttributes (true);
183 } catch (NotSupportedException
) { }
185 ginst
.GetCustomAttributes (typeof (int), true);
187 } catch (NotSupportedException
) { }