2 // MethodOnTypeBuilderInstTest - NUnit Test Cases for MethodOnTypeBuilderInst
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // Copyright (C) 2008 Gert Driesen
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Globalization;
34 using System.Reflection;
35 using System.Reflection.Emit;
37 using NUnit.Framework;
39 namespace MonoTests.System.Reflection.Emit
42 public class MethodOnTypeBuilderInstTest
44 private static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.MethodOnTypeBuilderInstTest";
46 private AssemblyBuilder assembly;
47 private ModuleBuilder module;
48 private MethodBuilder mb_create;
49 private MethodBuilder mb_edit;
50 private Type typeBarOfT;
51 private Type typeBarOfInt32;
52 private MethodInfo method_create;
53 private MethodInfo method_edit;
58 AssemblyName assemblyName = new AssemblyName ();
59 assemblyName.Name = ASSEMBLY_NAME;
61 assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
62 assemblyName, AssemblyBuilderAccess.RunAndSave,
65 module = assembly.DefineDynamicModule ("module1");
67 TypeBuilder tb = module.DefineType ("Bar");
68 GenericTypeParameterBuilder [] typeParams = tb.DefineGenericParameters ("T");
70 ConstructorBuilder cb = tb.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
71 ILGenerator ig = cb.GetILGenerator ();
72 ig.Emit (OpCodes.Ret);
74 typeBarOfT = tb.MakeGenericType (typeParams [0]);
76 mb_create = tb.DefineMethod ("create",
77 MethodAttributes.Public | MethodAttributes.Static,
78 typeBarOfT, Type.EmptyTypes);
79 ig = mb_create.GetILGenerator ();
80 ig.Emit (OpCodes.Newobj, TypeBuilder.GetConstructor (
82 ig.Emit (OpCodes.Ret);
84 mb_edit = tb.DefineMethod ("edit",
85 MethodAttributes.Public | MethodAttributes.Static,
86 typeBarOfT, Type.EmptyTypes);
87 ig = mb_edit.GetILGenerator ();
88 ig.Emit (OpCodes.Newobj, TypeBuilder.GetConstructor (
90 ig.Emit (OpCodes.Ret);
91 mb_edit.SetParameters (mb_edit.DefineGenericParameters ("X"));
93 typeBarOfInt32 = tb.MakeGenericType (typeof (int));
95 method_create = TypeBuilder.GetMethod (typeBarOfInt32, mb_create);
96 method_edit = TypeBuilder.GetMethod (typeBarOfInt32, mb_edit);
100 public void Attributes ()
102 MethodAttributes attrs;
104 attrs = method_create.Attributes;
105 Assert.AreEqual (MethodAttributes.PrivateScope |
106 MethodAttributes.Public | MethodAttributes.Static,
108 attrs = method_edit.Attributes;
109 Assert.AreEqual (MethodAttributes.PrivateScope |
110 MethodAttributes.Public | MethodAttributes.Static,
115 public void CallingConvention ()
117 CallingConventions conv;
119 conv = method_create.CallingConvention;
120 Assert.AreEqual (CallingConventions.Standard, conv, "#1");
121 conv = method_edit.CallingConvention;
122 Assert.AreEqual (CallingConventions.Standard, conv, "#2");
126 [Category ("NotWorking")]
127 public void ContainsGenericParameters ()
130 bool genparam = method_create.ContainsGenericParameters;
131 Assert.Fail ("#A1:" + genparam);
132 } catch (NotSupportedException ex) {
133 // Specified method is not supported
134 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
135 Assert.IsNull (ex.InnerException, "#A3");
136 Assert.IsNotNull (ex.Message, "#A4");
140 bool genparam = method_edit.ContainsGenericParameters;
141 Assert.Fail ("#B1:" + genparam);
142 } catch (NotSupportedException ex) {
143 // Specified method is not supported
144 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
145 Assert.IsNull (ex.InnerException, "#B3");
146 Assert.IsNotNull (ex.Message, "#B4");
151 public void DeclaringType ()
153 Assert.AreSame (typeBarOfInt32, method_create.DeclaringType, "#1");
154 Assert.AreSame (typeBarOfInt32, method_edit.DeclaringType, "#2");
158 [Category ("NotWorking")]
159 public void GetBaseDefinition ()
162 method_create.GetBaseDefinition ();
164 } catch (NotSupportedException ex) {
165 // Specified method is not supported
166 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
167 Assert.IsNull (ex.InnerException, "#A3");
168 Assert.IsNotNull (ex.Message, "#A4");
172 method_edit.GetBaseDefinition ();
174 } catch (NotSupportedException ex) {
175 // Specified method is not supported
176 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
177 Assert.IsNull (ex.InnerException, "#B3");
178 Assert.IsNotNull (ex.Message, "#B4");
182 [Test] // GetCustomAttributes (Boolean)
183 [Category ("NotWorking")]
184 public void GetCustomAttributes1 ()
187 method_create.GetCustomAttributes (false);
189 } catch (NotSupportedException ex) {
190 // The invoked member is not supported in a dynamic module
191 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
192 Assert.IsNull (ex.InnerException, "#A3");
193 Assert.IsNotNull (ex.Message, "#A4");
197 method_edit.GetCustomAttributes (false);
199 } catch (NotSupportedException ex) {
200 // The invoked member is not supported in a dynamic module
201 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
202 Assert.IsNull (ex.InnerException, "#B3");
203 Assert.IsNotNull (ex.Message, "#B4");
207 [Test] // GetCustomAttributes (Type, Boolean)
208 [Category ("NotWorking")]
209 public void GetCustomAttributes2 ()
212 method_create.GetCustomAttributes (typeof (FlagsAttribute), false);
214 } catch (NotSupportedException ex) {
215 // The invoked member is not supported in a dynamic module
216 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
217 Assert.IsNull (ex.InnerException, "#A3");
218 Assert.IsNotNull (ex.Message, "#A4");
222 method_edit.GetCustomAttributes (typeof (FlagsAttribute), false);
224 } catch (NotSupportedException ex) {
225 // The invoked member is not supported in a dynamic module
226 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
227 Assert.IsNull (ex.InnerException, "#B3");
228 Assert.IsNotNull (ex.Message, "#B4");
233 [Category ("NotWorking")]
234 public void GetGenericArguments ()
238 args = method_create.GetGenericArguments ();
239 Assert.IsNull (args, "#A");
241 args = method_edit.GetGenericArguments ();
242 Assert.IsNotNull (args, "#B1");
243 Assert.AreEqual (1, args.Length, "#B2");
244 Assert.AreEqual ("X", args [0].Name, "#B3");
248 [Category ("NotWorking")]
249 public void GetGenericMethodDefinition ()
251 MethodInfo method_def;
253 method_def = method_create.GetGenericMethodDefinition ();
254 Assert.IsNotNull (method_def, "#A1");
255 Assert.AreSame (mb_create, method_def, "#A2");
257 method_def = method_edit.GetGenericMethodDefinition ();
258 Assert.IsNotNull (method_def, "#B1");
259 Assert.AreSame (mb_edit, method_def, "#B2");
263 public void GetMethodImplementationFlags ()
265 MethodImplAttributes flags;
267 flags = method_create.GetMethodImplementationFlags ();
268 Assert.AreEqual (MethodImplAttributes.Managed, flags, "#1");
269 flags = method_edit.GetMethodImplementationFlags ();
270 Assert.AreEqual (MethodImplAttributes.Managed, flags, "#2");
274 [Category ("NotWorking")]
275 public void GetParameters ()
278 method_create.GetParameters ();
280 } catch (NotSupportedException ex) {
281 // Type has not been created
282 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
283 Assert.IsNull (ex.InnerException, "#A3");
284 Assert.IsNotNull (ex.Message, "#A4");
288 method_edit.GetParameters ();
290 } catch (NotSupportedException ex) {
291 // Type has not been created
292 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
293 Assert.IsNull (ex.InnerException, "#B3");
294 Assert.IsNotNull (ex.Message, "#B4");
299 [Category ("NotWorking")]
300 public void Invoke ()
303 method_create.Invoke (null, BindingFlags.Default, null,
304 new object [0], CultureInfo.InvariantCulture);
306 } catch (NotSupportedException ex) {
307 // Specified method is not supported
308 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
309 Assert.IsNull (ex.InnerException, "#A3");
310 Assert.IsNotNull (ex.Message, "#A4");
314 method_edit.Invoke (null, BindingFlags.Default, null,
315 new object [0], CultureInfo.InvariantCulture);
317 } catch (NotSupportedException ex) {
318 // Specified method is not supported
319 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
320 Assert.IsNull (ex.InnerException, "#B3");
321 Assert.IsNotNull (ex.Message, "#B4");
326 [Category ("NotWorking")]
327 public void IsDefined ()
330 method_create.IsDefined (typeof (FlagsAttribute), false);
332 } catch (NotSupportedException ex) {
333 // The invoked member is not supported in a dynamic module
334 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
335 Assert.IsNull (ex.InnerException, "#A3");
336 Assert.IsNotNull (ex.Message, "#A4");
340 method_edit.IsDefined (typeof (FlagsAttribute), false);
342 } catch (NotSupportedException ex) {
343 // The invoked member is not supported in a dynamic module
344 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
345 Assert.IsNull (ex.InnerException, "#B3");
346 Assert.IsNotNull (ex.Message, "#B4");
351 public void IsGenericMethodDefinition ()
353 Assert.IsFalse (method_create.IsGenericMethodDefinition, "#1");
354 Assert.IsTrue (method_edit.IsGenericMethodDefinition, "#2");
358 public void IsGenericMethod ()
360 Assert.IsFalse (method_create.IsGenericMethod, "#1");
361 Assert.IsTrue (method_edit.IsGenericMethod, "#2");
365 [Category ("NotWorking")]
366 public void MakeGenericMethod ()
369 method_create.MakeGenericMethod (typeof (int));
371 } catch (InvalidOperationException ex) {
372 // create is not a GenericMethodDefinition.
373 // MakeGenericMethod may only be called on a
374 // method for which MethodBase.IsGenericMethodDefinition
376 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
377 Assert.IsNull (ex.InnerException, "#A3");
378 Assert.IsNotNull (ex.Message, "#A4");
381 MethodInfo genEdit = method_edit.MakeGenericMethod (typeof (int));
382 Assert.IsFalse (genEdit.ContainsGenericParameters, "#B1");
383 Assert.IsTrue (genEdit.IsGenericMethod, "#B2");
384 Assert.IsFalse (genEdit.IsGenericMethodDefinition, "#B3");
388 public void MemberType ()
390 Assert.AreEqual (MemberTypes.Method, method_create.MemberType, "#1");
391 Assert.AreEqual (MemberTypes.Method, method_edit.MemberType, "#2");
395 [Category ("NotWorking")]
396 public void MethodHandle ()
399 RuntimeMethodHandle handle = method_create.MethodHandle;
400 Assert.Fail ("#A1:" + handle);
401 } catch (NotSupportedException ex) {
402 // The invoked member is not supported in a dynamic module
403 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
404 Assert.IsNull (ex.InnerException, "#A3");
405 Assert.IsNotNull (ex.Message, "#A4");
409 RuntimeMethodHandle handle = method_edit.MethodHandle;
410 Assert.Fail ("#B1:" + handle);
411 } catch (NotSupportedException ex) {
412 // The invoked member is not supported in a dynamic module
413 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
414 Assert.IsNull (ex.InnerException, "#B3");
415 Assert.IsNotNull (ex.Message, "#B4");
420 public void Module ()
422 Assert.AreSame (module, method_create.Module, "#1");
423 Assert.AreSame (module, method_edit.Module, "#2");
429 Assert.AreEqual ("create", method_create.Name, "#1");
430 Assert.AreEqual ("edit", method_edit.Name, "#2");
434 public void ReflectedType ()
436 Assert.AreSame (typeBarOfInt32, method_create.ReflectedType, "#1");
437 Assert.AreSame (typeBarOfInt32, method_edit.ReflectedType, "#2");
441 public void ReturnParameter ()
444 ParameterInfo ret = method_create.ReturnParameter;
445 Assert.Fail ("#A1:" + (ret != null));
446 } catch (NotSupportedException ex) {
447 // Specified method is not supported
448 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
449 Assert.IsNull (ex.InnerException, "#A3");
450 Assert.IsNotNull (ex.Message, "#A4");
454 ParameterInfo ret = method_create.ReturnParameter;
455 Assert.Fail ("#B1:" + (ret != null));
456 } catch (NotSupportedException ex) {
457 // Specified method is not supported
458 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
459 Assert.IsNull (ex.InnerException, "#B3");
460 Assert.IsNotNull (ex.Message, "#B4");
465 [Category ("NotWorking")]
466 public void ReturnType ()
470 ret = method_create.ReturnType;
471 Assert.AreSame (typeBarOfT, ret, "#1");
472 ret = method_edit.ReturnType;
473 Assert.AreSame (typeBarOfT, ret, "#2");
477 [Category ("NotWorking")]
478 public void ReturnTypeCustomAttributes ()
481 ICustomAttributeProvider attr_prov = method_create.ReturnTypeCustomAttributes;
482 Assert.Fail ("#A1:" + (attr_prov != null));
483 } catch (NotSupportedException ex) {
484 // Specified method is not supported
485 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
486 Assert.IsNull (ex.InnerException, "#A3");
487 Assert.IsNotNull (ex.Message, "#A4");
491 ICustomAttributeProvider attr_prov = method_edit.ReturnTypeCustomAttributes;
492 Assert.Fail ("#B1:" + (attr_prov != null));
493 } catch (NotSupportedException ex) {
494 // Specified method is not supported
495 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
496 Assert.IsNull (ex.InnerException, "#B3");
497 Assert.IsNotNull (ex.Message, "#B4");