Merge pull request #4503 from BrzVlad/fix-appdomain-unload
[mono.git] / mcs / class / corlib / Test / System.Reflection / MethodInfoTest.cs
1 //
2 // System.Reflection.MethodInfo Test Cases
3 //
4 // Authors:
5 //  Zoltan Varga (vargaz@gmail.com)
6 //  Aleksey Kliger (aleksey@xamarin.com)
7 //
8 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2015 Xamarin, Inc. (http://www.xamarin.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using NUnit.Framework;
33 using System;
34 using System.Threading;
35 using System.Reflection;
36 #if !MONOTOUCH && !FULL_AOT_RUNTIME
37 using System.Reflection.Emit;
38 #endif
39 using System.Runtime.InteropServices;
40 using System.Runtime.CompilerServices;
41
42 using System.Collections.Generic;
43
44 namespace A.B.C {
45         // Disable expected warning
46 #pragma warning disable 169
47         public struct MethodInfoTestStruct {
48                 int p;
49         }
50 #pragma warning restore 169
51 }
52 namespace MonoTests.System.Reflection
53 {
54         [TestFixture]
55         public class MethodInfoTest
56         {
57 #if MONOTOUCH || FULL_AOT_RUNTIME
58                 // use an existing symbol - so we can build without dlsym. It does not matter that the signature does not match for the test
59                 [DllImport ("libc", EntryPoint="readlink", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
60 #else
61                 [DllImport ("libfoo", EntryPoint="foo", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
62 #endif
63                 public static extern void dllImportMethod ();
64                 [MethodImplAttribute(MethodImplOptions.PreserveSig)]
65                 public void preserveSigMethod ()
66                 {
67                 }
68
69                 [MethodImplAttribute(MethodImplOptions.Synchronized)]
70                 public void synchronizedMethod ()
71                 {
72                 }
73
74                 public interface InterfaceTest
75                 {
76                         void Clone ();
77                 }
78
79                 [Test]
80                 public void IsDefined_AttributeType_Null ()
81                 {
82                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
83
84                         try {
85                                 mi.IsDefined ((Type) null, false);
86                                 Assert.Fail ("#1");
87                         } catch (ArgumentNullException ex) {
88                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
89                                 Assert.IsNull (ex.InnerException, "#3");
90                                 Assert.IsNotNull (ex.Message, "#4");
91                                 Assert.IsNotNull (ex.ParamName, "#5");
92                                 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
93                         }
94                 }
95
96                 [Test]
97                 public void TestInvokeByRefReturnMethod ()
98                 {
99                         try {
100                                 MethodInfo m = typeof (int[]).GetMethod ("Address");
101                                 m.Invoke (new int[1], new object[] { 0 });
102                                 Assert.Fail ("#1");
103                         } catch (NotSupportedException e) {
104                                 Assert.AreEqual (typeof (NotSupportedException), e.GetType (), "#2");
105                                 Assert.IsNull (e.InnerException, "#3");
106                                 Assert.IsNotNull (e.Message, "#4");
107                         }
108                 }
109
110                 [Test]
111                 public void PseudoCustomAttributes ()
112                 {
113                         Type t = typeof (MethodInfoTest);
114
115                         DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
116
117                         Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
118 #if MONOTOUCH || FULL_AOT_RUNTIME
119                         Assert.AreEqual ("readlink", attr.EntryPoint, "#2");
120                         Assert.AreEqual ("libc", attr.Value, "#3");
121 #else
122                         Assert.AreEqual ("foo", attr.EntryPoint, "#2");
123                         Assert.AreEqual ("libfoo", attr.Value, "#3");
124 #endif
125                         Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
126                         Assert.AreEqual (false, attr.ExactSpelling, "#5");
127                         Assert.AreEqual (true, attr.PreserveSig, "#6");
128                         Assert.AreEqual (true, attr.SetLastError, "#7");
129                         Assert.AreEqual (true, attr.BestFitMapping, "#8");
130                         Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
131
132                         PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
133
134                         // This doesn't work under MS.NET
135                         /*
136                           MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
137                         */
138                 }
139
140                 [return: MarshalAs (UnmanagedType.Interface)]
141                 public void ReturnTypeMarshalAs ()
142                 {
143                 }
144
145                 [Test]
146                 public void ReturnTypePseudoCustomAttributes ()
147                 {
148                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
149
150                         Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
151                 }
152
153                 public static int foo (int i, int j)
154                 {
155                         return i + j;
156                 }
157
158                 [Test]
159                 public void StaticInvokeWithObject ()
160                 {
161                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
162                         
163                         mi.Invoke (new Object (), new object [] { 1, 2 });
164                 }
165
166                 [Test]
167                 public void ByRefInvoke ()
168                 {
169                         MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
170                         object[] parms = new object[] {1};
171                         met.Invoke (null, parms);
172                         Assert.AreEqual (2, parms[0]);
173                 }
174
175                 public static void ByRefTest (ref int a1)
176                 {
177                         if (a1 == 1)
178                                 a1 = 2;
179                 }
180
181                 static int byref_arg;
182
183                 public static void ByrefVtype (ref int i) {
184                         byref_arg = i;
185                         i = 5;
186                 }
187
188                 [Test]
189                 public void ByrefVtypeInvoke ()
190                 {
191                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
192
193                         object o = 1;
194                         object[] args = new object [] { o };
195                         mi.Invoke (null, args);
196                         Assert.AreEqual (1, byref_arg, "#A1");
197                         Assert.AreEqual (1, o, "#A2");
198                         Assert.AreEqual (5, args[0], "#A3");
199
200                         args [0] = null;
201                         mi.Invoke (null, args);
202                         Assert.AreEqual (0, byref_arg, "#B1");
203                         Assert.AreEqual (5, args[0], "#B2");
204                 }
205
206                 public void HeyHey (out string out1, ref DateTime ref1)
207                 {
208                         out1 = null;
209                 }
210
211                 public void SignatureTest (__arglist)
212                 {
213                 }
214                 
215                 public static unsafe int* PtrFunc (int* a)
216                 {
217                         return (int*) 0;
218                 }
219
220 #if MONO_FEATURE_THREAD_ABORT
221                 [Test] // bug #81538
222                 public void InvokeThreadAbort ()
223                 {
224                         MethodInfo method = typeof (MethodInfoTest).GetMethod ("AbortIt");
225                         try {
226                                 method.Invoke (null, new object [0]);
227                                 Assert.Fail ("#1");
228                         }
229                         catch (ThreadAbortException ex) {
230                                 Thread.ResetAbort ();
231                                 Assert.IsNull (ex.InnerException, "#2");
232                         }
233                 }
234
235                 public static void AbortIt ()
236                 {
237                         Thread.CurrentThread.Abort ();
238                 }
239 #endif
240
241                 [Test] // bug #76541
242                 public void ToStringByRef ()
243                 {
244                         Assert.AreEqual ("Void HeyHey(System.String ByRef, System.DateTime ByRef)",
245                                 this.GetType ().GetMethod ("HeyHey").ToString ());
246                 }
247                 
248                 [Test]
249                 public void ToStringArgList ()
250                 {
251                         Assert.AreEqual ("Void SignatureTest(...)",
252                                 this.GetType ().GetMethod ("SignatureTest").ToString ());
253                 }
254
255                 [Test]
256                 public void ToStringWithPointerSignatures () //bug #409583
257                 {
258                         Assert.AreEqual ("Int32* PtrFunc(Int32*)", this.GetType ().GetMethod ("PtrFunc").ToString ());
259                 }
260
261                 public struct SimpleStruct
262                 {
263                         public int a;
264                 }
265
266                 public static unsafe SimpleStruct* PtrFunc2 (SimpleStruct* a, A.B.C.MethodInfoTestStruct *b)
267                 {
268                         return (SimpleStruct*) 0;
269                 }
270
271                 [Test]
272                 public void ToStringWithPointerSignaturesToNonPrimitiveType ()
273                 {
274                         Assert.AreEqual ("SimpleStruct* PtrFunc2(SimpleStruct*, A.B.C.MethodInfoTestStruct*)", 
275                                 this.GetType ().GetMethod ("PtrFunc2").ToString ());
276                 }       
277                 [Test]
278                 public void ToStringGenericMethod ()
279                 {
280                         Assert.AreEqual ("System.Collections.ObjectModel.ReadOnlyCollection`1[T] AsReadOnly[T](T[])",
281                                 typeof (Array).GetMethod ("AsReadOnly").ToString ());
282                 }
283
284                 class GBD_A         { public virtual     void f () {} }
285                 class GBD_B : GBD_A { public override    void f () {} }
286                 class GBD_C : GBD_B { public override    void f () {} }
287                 class GBD_D : GBD_C { public new virtual void f () {} }
288                 class GBD_E : GBD_D { public override    void f () {} }
289
290                 class GBD_E2 : GBD_D { }
291                 class GBD_F : GBD_E { }
292
293
294                 [Test]
295                 public void GetBaseDefinition ()
296                 {
297                         Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
298                         Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().ReflectedType, "#1r");
299
300                         Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
301                         Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().ReflectedType, "#2r");
302
303                         Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
304                         Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().ReflectedType, "#3r");
305
306                         Assert.AreEqual (typeof (GBD_D), typeof (GBD_E2).GetMethod ("f").GetBaseDefinition ().DeclaringType, "#4");
307                         Assert.AreEqual (typeof (GBD_D), typeof (GBD_E2).GetMethod ("f").GetBaseDefinition ().ReflectedType, "#4r");
308
309                         Assert.AreEqual (typeof (GBD_D), typeof (GBD_F).GetMethod ("f").GetBaseDefinition ().DeclaringType, "#5");
310                         Assert.AreEqual (typeof (GBD_D), typeof (GBD_F).GetMethod ("f").GetBaseDefinition ().ReflectedType, "#5r");
311
312                 }
313
314                 class GenericBase<T,H> {
315                         public virtual void f2 () { }
316                 }
317
318                 class GenericMid<T, U> : GenericBase<T, Action<U>> {
319                         public virtual T f1 () { return default (T); }
320                 }
321
322                 class GenericChild<T> : GenericMid<T, int> {
323                         public override T f1 () { return default (T); }
324                         public override void f2 () { }
325                 }
326
327                 class DerivedFromGenericBase : GenericBase<int, int> {
328                 }
329
330                 [Test]
331                 public void GetBaseDefinition_OpenConstructedBaseType () // 36305
332                 {
333                         var t = typeof (GenericChild<string>);
334
335                         var mi1 = t.GetMethod ("f1");
336                         var mi1_base = mi1.GetBaseDefinition ();
337
338                         Assert.AreEqual (typeof (GenericMid<string, int>), mi1_base.DeclaringType, "#1");
339
340                         var mi2 = t.GetMethod ("f2");
341                         var mi2_base = mi2.GetBaseDefinition ();
342
343                         Assert.AreEqual (typeof (GenericBase<string, Action<int>>), mi2_base.DeclaringType, "#2");
344                 }
345
346                 class TestInheritedMethodA {
347                         private void TestMethod ()
348                         {
349                         }
350
351                         public void TestMethod2 ()
352                         {
353                         }
354                 }
355
356                 class TestInheritedMethodB : TestInheritedMethodA {
357                 }
358
359                 [Test]
360                 public void InheritanceTestGetMethodTest ()
361                 {
362                         MethodInfo inheritedMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
363                         MethodInfo baseMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
364                         Assert.AreSame (inheritedMethod, baseMethod);
365
366                         MethodInfo inheritedMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
367                         MethodInfo baseMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
368                         Assert.AreSame (inheritedMethod, baseMethod);
369                 }
370
371                 [Test]
372                 public void GetMethodBody_Abstract ()
373                 {
374                         MethodBody mb = typeof (InterfaceTest).GetMethod ("Clone").GetMethodBody ();
375                         Assert.IsNull (mb);
376                 }
377
378                 [Test]
379                 public void GetMethodBody_Runtime ()
380                 {
381                         MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
382                         Assert.IsNull (mb);
383                 }
384
385                 [Test]
386                 public void GetMethodBody_Pinvoke ()
387                 {
388                         MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
389                         Assert.IsNull (mb);
390                 }
391
392                 [Test]
393                 public void GetMethodBody_Icall ()
394                 {
395                         foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
396                                 if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
397                                         MethodBody mb = mi.GetMethodBody ();
398                                         Assert.IsNull (mb);
399                                 }
400                 }
401
402                 public static void locals_method ()
403                 {
404                         byte[] b = new byte [10];
405
406                         unsafe {
407                                 /* This generates a pinned local */
408                                 fixed (byte *p = &b [0]) {
409                                 }
410                         }
411                 }
412
413                 [Test]
414                 public void GetMethodBody ()
415                 {
416 #if (MONOTOUCH || FULL_AOT_RUNTIME) && !DEBUG
417                         Assert.Ignore ("Release app (on devices) are stripped of (managed) IL so this test would fail");
418 #endif
419                         MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
420
421                         Assert.IsTrue (mb.InitLocals, "#1");
422                         Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
423
424                         IList<LocalVariableInfo> locals = mb.LocalVariables;
425
426                         bool foundPinnedBytePointer = false;
427                         unsafe {
428                                 foreach (LocalVariableInfo lvi in locals) {
429                                         if (lvi.LocalType == typeof (byte[]))
430                                                 // This is optimized out by CSC in .NET 4.6
431                                                 Assert.IsFalse (lvi.IsPinned, "#3-1");
432
433                                         if (/* mcs */ lvi.LocalType == typeof (byte*) || /* csc */ lvi.LocalType == typeof (byte).MakeByRefType ()) {
434                                                 foundPinnedBytePointer = true;
435                                                 Assert.IsTrue (lvi.IsPinned, "#3-2");
436                                         }
437                                 }
438                         }
439                         Assert.IsTrue (foundPinnedBytePointer, "#4");
440                 }
441
442                 public int return_parameter_test ()
443                 {
444                         return 0;
445                 }
446
447                 [Test]
448                 public void GetMethodFromHandle_Generic ()
449                 {
450                         MethodHandleTest<int> test = new MethodHandleTest<int> ();
451                         RuntimeMethodHandle mh = test.GetType ().GetProperty ("MyList")
452                                 .GetGetMethod ().MethodHandle;
453                         MethodBase mb = MethodInfo.GetMethodFromHandle (mh,
454                                 typeof (MethodHandleTest<int>).TypeHandle);
455                         Assert.IsNotNull (mb, "#1");
456                         List<int> list = (List<int>) mb.Invoke (test, null);
457                         Assert.IsNotNull (list, "#2");
458                         Assert.AreEqual (1, list.Count, "#3");
459                 }
460
461                 [Test]
462                 public void ReturnParameter ()
463                 {
464                         ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
465                         Assert.AreEqual (typeof (int), pi.ParameterType, "#1");
466                         Assert.AreEqual (-1, pi.Position, "#2");
467                         // MS always return false here
468                         //Assert.IsTrue (pi.IsRetval, "#3");
469                 }
470
471                 [Test]
472                 public void MethodInfoModule ()
473                 {
474                         Type type = typeof (MethodInfoTest);
475                         MethodInfo me = type.GetMethod ("return_parameter_test");
476
477                         Assert.AreEqual (type.Module, me.Module);
478                 }
479
480                 [Test]
481                         public void InvokeOnRefOnlyAssembly ()
482                 {
483                         Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
484                         Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
485                         MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
486                         try {
487                                 m.Invoke (null, new object [0]);
488                                 Assert.Fail ("#1");
489                         } catch (InvalidOperationException ex) {
490                                 // The requested operation is invalid in the
491                                 // ReflectionOnly context
492                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
493                                 Assert.IsNull (ex.InnerException, "#3");
494                                 Assert.IsNotNull (ex.Message, "#4");
495                         }
496                 }
497
498                 [Test]
499                 [ExpectedException (typeof (TargetInvocationException))]
500                 public void InvokeInvalidOpExceptionThrow () {
501                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ThrowMethod");
502                         mi.Invoke(null, null);
503                 }
504
505                 public static void ThrowMethod () {
506                         throw new InvalidOperationException ();
507                 }
508
509                 [Test]
510                 public void InvokeGenericVtype ()
511                 {
512                         KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
513                         Type type = kvp.GetType ();
514                         Type [] arguments = type.GetGenericArguments ();
515                         MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
516                         MethodInfo generic_method = method.MakeGenericMethod (arguments);
517                         kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
518
519                         Assert.AreEqual ("a", kvp.Key, "#1");
520                         Assert.AreEqual (21, kvp.Value, "#2");
521                 }
522
523                 public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
524                 {
525                         return kvp;
526                 }
527
528                 [Test] // bug #81997
529                 public void InvokeGenericInst ()
530                 {
531                         List<string> str = null;
532
533                         object [] methodArgs = new object [] { str };
534                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("GenericRefMethod");
535                         mi.Invoke (null, methodArgs);
536                         Assert.IsNotNull (methodArgs [0], "#A1");
537                         Assert.IsNull (str, "#A2");
538                         Assert.IsTrue (methodArgs [0] is List<string>, "#A3");
539
540                         List<string> refStr = methodArgs [0] as List<string>;
541                         Assert.IsNotNull (refStr, "#B1");
542                         Assert.AreEqual (1, refStr.Count, "#B2");
543                         Assert.AreEqual ("test", refStr [0], "#B3");
544                 }
545
546                 public static void GenericRefMethod (ref List<string> strArg)
547                 {
548                         strArg = new List<string> ();
549                         strArg.Add ("test");
550                 }
551
552                 public void MakeGenericMethodArgsMismatchFoo<T> ()
553                 {
554                 }
555
556                 [Test]
557                 public void MakeGenericMethodArgsMismatch ()
558                 {
559                         MethodInfo gmi = this.GetType ().GetMethod (
560                                 "MakeGenericMethodArgsMismatchFoo");
561                         try {
562                                 gmi.MakeGenericMethod ();
563                                 Assert.Fail ("#1");
564                         } catch (ArgumentException ex) {
565                                 // The type or method has 1 generic parameter(s),
566                                 // but 0 generic argument(s) were provided. A
567                                 // generic argument must be provided for each
568                                 // generic parameter
569                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
570                                 Assert.IsNull (ex.InnerException, "#3");
571                                 Assert.IsNotNull (ex.Message, "#4");
572                                 Assert.IsNull (ex.ParamName, "#5");
573                         }
574                 }
575
576                 public void SimpleGenericMethod<TFoo, TBar> () {}
577
578                 [Test]
579                 public void MakeGenericMethodWithNullArray ()
580                 {
581                         MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
582                         try {
583                                 gmi.MakeGenericMethod ((Type []) null);
584                                 Assert.Fail ("#1");
585                         } catch (ArgumentNullException ex) {
586                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
587                                 Assert.IsNull (ex.InnerException, "#3");
588                                 Assert.IsNotNull (ex.Message, "#4");
589                                 Assert.AreEqual ("methodInstantiation", ex.ParamName, "#5");
590                         }
591                 }
592
593                 [Test]
594                 public void MakeGenericMethodWithNullValueInTypesArray ()
595                 {
596                         MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
597                         try {
598                                 gmi.MakeGenericMethod (new Type [] { typeof (int), null });
599                                 Assert.Fail ("#1");
600                         } catch (ArgumentNullException ex) {
601                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
602                                 Assert.IsNull (ex.InnerException, "#3");
603                                 Assert.IsNotNull (ex.Message, "#4");
604                                 Assert.IsNull (ex.ParamName, "#5");
605                         }
606                 }
607
608                 [Test]
609                 public void MakeGenericMethodWithNonGenericMethodDefinitionMethod ()
610                 {
611                         MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
612                         MethodInfo inst = gmi.MakeGenericMethod (typeof (int), typeof (double));
613                         try {
614                                 inst.MakeGenericMethod (typeof (int), typeof (double));
615                                 Assert.Fail ("#1");
616                         } catch (InvalidOperationException ex) {
617                         }
618                 }
619 #if !MONOTOUCH && !FULL_AOT_RUNTIME
620                 public TFoo SimpleGenericMethod2<TFoo, TBar> () { return default (TFoo); }
621                 /*Test for the uggly broken behavior of SRE.*/
622                 [Test]
623                 public void MakeGenericMethodWithSreTypeResultsInStupidMethodInfo ()
624                 {
625                         AssemblyName assemblyName = new AssemblyName ();
626                         assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodInfoTest";
627                         AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
628                         ModuleBuilder module = assembly.DefineDynamicModule ("module1", "tst.dll");
629                         TypeBuilder tb = module.DefineType ("Test", TypeAttributes.Public);
630
631                         MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod2");
632                         MethodInfo ins = gmi.MakeGenericMethod (typeof (int), tb);
633
634                         Assert.AreSame (tb, ins.GetGenericArguments () [1], "#1");
635                         /*broken ReturnType*/
636                         Assert.AreSame (gmi.GetGenericArguments () [0], ins.ReturnType, "#2");
637                 }
638 #endif
639                 public static int? pass_nullable (int? i)
640                 {
641                         return i;
642                 }
643
644                 [Test]
645                 public void NullableTests ()
646                 {
647                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
648                         Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
649                         Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
650
651                         // Test conversion of vtype to a nullable type for the this argument
652                         PropertyInfo pi = typeof (Nullable <int>).GetProperty ("HasValue");
653                         Assert.AreEqual (true, pi.GetGetMethod ().Invoke (10, null));
654                         PropertyInfo pi2 = typeof (Nullable <int>).GetProperty ("Value");
655                         Assert.AreEqual (10, pi2.GetGetMethod ().Invoke (10, null));
656                 }
657
658                 public static void foo_generic<T> ()
659                 {
660                 }
661
662                 [Test]
663                 public void IsGenericMethod ()
664                 {
665                         MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
666                         Assert.AreEqual (true, mi.IsGenericMethod, "#1");
667                         MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
668                         Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
669                         MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
670                         Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
671                 }
672
673                 class A<T>
674                 {
675                         public static void Foo<T2> (T2 i)
676                         {
677                         }
678
679                         public static void Bar ()
680                         {
681                         }
682
683                         public class B
684                         {
685                                 public static void Baz ()
686                                 {
687                                 }
688                         }
689                 }
690
691                 [Test]
692                 public void ContainsGenericParameters ()
693                 {
694                         // Non-generic method in open generic type
695                         Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
696                         // open generic method in closed generic type
697                         Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
698                         // non-generic method in closed generic type
699                         Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
700                         // closed generic method in closed generic type
701                         Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
702                         // non-generic method in non-generic nested type of closed generic type
703                         Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
704                         // non-generic method in non-generic nested type of open generic type
705                         Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
706                 }
707
708                 [Test]
709                 public void IsGenericMethodDefinition ()
710                 {
711                         MethodInfo m1 = typeof (A<>).GetMethod ("Foo");
712                         Assert.IsTrue (m1.IsGenericMethod, "#A1");
713                         Assert.IsTrue (m1.IsGenericMethodDefinition, "#A2");
714
715                         MethodInfo m2 = typeof (A<int>).GetMethod ("Foo");
716                         Assert.IsTrue (m2.IsGenericMethod, "#B1");
717                         Assert.IsTrue (m2.IsGenericMethodDefinition, "#B2");
718
719                         MethodInfo m3 = m2.MakeGenericMethod (typeof (int));
720                         Assert.IsTrue (m3.IsGenericMethod, "#C1");
721                         Assert.IsFalse (m3.IsGenericMethodDefinition, "#C2");
722                 }
723
724                 [Test]
725                 public void GetGenericMethodDefinition ()
726                 {
727                         MethodInfo mi1 = typeof (MyList<>).GetMethod ("ConvertAll");
728                         MethodInfo mi2 = typeof (MyList<int>).GetMethod ("ConvertAll");
729
730                         Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[T,TOutput]",
731                                          mi1.GetParameters () [0].ParameterType.ToString (), "#A1");
732                         Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[System.Int32,TOutput]",
733                                          mi2.GetParameters () [0].ParameterType.ToString (), "#A2");
734                         Assert.IsTrue (mi1.IsGenericMethod, "#A3");
735                         Assert.IsTrue (mi1.IsGenericMethodDefinition, "#A4");
736                         Assert.IsTrue (mi2.IsGenericMethod, "#A5");
737                         Assert.IsTrue (mi2.IsGenericMethodDefinition, "#A6");
738
739                         MethodInfo mi3 = mi2.GetGenericMethodDefinition ();
740
741                         Assert.IsTrue (mi3.IsGenericMethod, "#B1");
742                         Assert.IsTrue (mi3.IsGenericMethodDefinition, "#B2");
743                         Assert.AreSame (mi2, mi3, "#B3");
744
745                         MethodInfo mi4 = mi2.MakeGenericMethod (typeof (short));
746                         Assert.IsTrue (mi4.IsGenericMethod, "#C1");
747                         Assert.IsFalse (mi4.IsGenericMethodDefinition, "#C2");
748                         Assert.AreSame (mi2, mi4.GetGenericMethodDefinition (), "#C3");
749                 }
750
751                 public void TestMethod123(int a, int b) {}
752
753                 [Test]
754                 public void GetParametersDontReturnInternedArray ()
755                 {
756                         var method = typeof (MethodInfoTest).GetMethod ("TestMethod123");
757                         var parms = method.GetParameters ();
758                         Assert.AreNotSame (parms, method.GetParameters (), "#1");
759
760                         parms [0] = null;
761                         Assert.IsNotNull (method.GetParameters () [0], "#2");
762                 }
763
764                 [Test]
765                 public void Bug354757 ()
766                 {
767                         MethodInfo gmd = (typeof (MyList <int>)).GetMethod ("ConvertAll");
768                         MethodInfo oi = gmd.MakeGenericMethod (gmd.GetGenericArguments ());
769                         Assert.AreSame (gmd, oi);
770                 }
771
772                 [Test]
773                 [ExpectedException (typeof (ArgumentException))]
774 #if MOBILE
775                 [Category ("NotWorking")] // #10552
776 #endif
777                 public void MakeGenericMethodRespectConstraints ()
778                 {
779                         var m = typeof (MethodInfoTest).GetMethod ("TestMethod");
780                         m.MakeGenericMethod (typeof (Type));
781                 }
782
783                 public void TestMethod <T> () where T : Exception
784                 {
785                 }
786
787                 public class MyList<T>
788                 {
789                         public TOutput ConvertAll<TOutput> (Foo<T,TOutput> arg)
790                         {
791                                 return default (TOutput);
792                         }
793                         public T ConvertAll2 (MyList<T> arg)
794                         {
795                                 return default (T);
796                         }
797                 }
798
799                 public class Foo<T,TOutput>
800                 {
801                 }
802
803                 class GenericHelper<T>
804                 {
805                         public void Test (T t)
806                         {
807                         }
808                 }
809                 interface IMethodInvoke<out T>
810                 {
811                     T Test ();
812                 }
813
814                 class MethodInvoke : IMethodInvoke<string>
815                 {
816                     public string Test ()
817                     {
818                         return "MethodInvoke";
819                     }
820                 }
821
822                 [Test]
823                 public void GetInterfaceMapWorksWithVariantIfaces ()
824                 {
825                         var m0 = typeof (IMethodInvoke<object>).GetMethod ("Test");
826                         var m1 = typeof (IMethodInvoke<string>).GetMethod ("Test");
827                         var obj = new MethodInvoke ();
828
829                         Assert.AreEqual ("MethodInvoke", m0.Invoke (obj, new Object [0]));
830                         Assert.AreEqual ("MethodInvoke", m1.Invoke (obj, new Object [0]));
831                 }
832
833
834                 public int? Bug12856 ()
835                 {
836                         return null;
837                 }
838
839                 [Test] //Bug #12856
840                 public void MethodToStringShouldPrintFullNameOfGenericStructs ()
841                 {
842                         var m = GetType ().GetMethod ("Bug12856");
843                         Assert.AreEqual ("System.Nullable`1[System.Int32] Bug12856()", m.ToString (), "#1");
844                 }
845
846                 [Test]
847                 public void GetReflectedType () // #12205
848                 {
849                         // public method declared in base type, queried from a derived type
850                         MethodInfo mi = typeof (TestInheritedMethodB).GetMethod ("TestMethod2");
851                         Assert.AreEqual (mi.ReflectedType, typeof (TestInheritedMethodB), "#1");
852
853                         // public method declared in a generic class,
854                         // queried from a non-generic class derived
855                         // from an instantiation of the generic class.
856                         mi = typeof (DerivedFromGenericBase).GetMethod ("f2");
857                         Assert.AreEqual (mi.ReflectedType, typeof (DerivedFromGenericBase), "#2");
858
859                         // public method declared in a generic class,
860                         // queried from the generic type defintion of
861                         // a generic derived class.
862                         mi = typeof (GenericMid<,>).GetMethod ("f2");
863                         Assert.AreEqual (mi.ReflectedType, typeof (GenericMid<,>), "#3");
864
865                         // public method declared in a generic class,
866                         // queried from an instantiation of a generic
867                         // derived class.
868                         mi = typeof (GenericMid<int,int>).GetMethod ("f2");
869                         Assert.AreEqual (mi.ReflectedType, typeof (GenericMid<int,int>), "#4");
870
871                 }
872
873 #if !MONOTOUCH && !FULL_AOT_RUNTIME
874                 class GenericClass<T>
875                 {
876                         public void Method ()
877                         {
878                                 T lv = default(T);
879                                 Console.WriteLine(lv);
880                         }
881
882                         public void Method2<K> (T a0, K a1)
883                         {
884                                 T var0 = a0;
885                                 K var1 = a1;
886                                 Console.WriteLine (var0);
887                                 Console.WriteLine (var1);
888                         }
889                 }
890
891                 [Test]
892                 public void TestLocalVariableTypes ()
893                 {
894                         var typeofT = typeof (GenericClass<>).GetGenericArguments () [0];
895                         var typeofK = typeof (GenericClass<>).GetMethod ("Method2").GetGenericArguments () [0];
896
897                         var type = typeof (GenericClass<>).GetMethod("Method").GetMethodBody().LocalVariables[0].LocalType;
898                         Assert.AreEqual (typeofT, type);
899                         Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
900                 
901                         bool foundTypeOfK = false;
902                         bool foundExpectedType = false;
903             
904                         MethodBody mb = typeof (GenericClass<>).GetMethod("Method2").GetMethodBody();
905                         foreach (LocalVariableInfo lvi in mb.LocalVariables) {
906                                 if (lvi.LocalType == typeofK) {
907                                         foundTypeOfK = true;
908                                         Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#1-1");
909                                 } else if (lvi.LocalType == typeofT) {
910                                         foundExpectedType = true;
911                                         Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#1-2");
912                                 }
913                         }
914
915                         Assert.IsTrue (foundTypeOfK, "#1-3");
916                         if (mb.LocalVariables.Count < 2)
917                                 Assert.Ignore ("Code built in release mode - 'T var0' optmized out");
918                         else
919                                 Assert.IsTrue (foundExpectedType, "#1-4");
920             
921                         foundTypeOfK = false;
922                         foundExpectedType = false;
923                         mb = typeof (GenericClass<int>).GetMethod("Method2").GetMethodBody();
924                         foreach (LocalVariableInfo lvi in mb.LocalVariables) {
925                                 if (lvi.LocalType == typeofK) {
926                                         foundTypeOfK = true;
927                                         Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#2-1");
928                                 } else if (lvi.LocalType == typeof (int)) {
929                                         foundExpectedType = true;
930                                 }
931                         }
932             
933                         Assert.IsTrue (foundTypeOfK, "#2-3");
934                         if (mb.LocalVariables.Count < 2)
935                                 Assert.Ignore ("Code built in release mode - 'int var0' optmized out");
936                         else
937                                 Assert.IsTrue (foundExpectedType, "#2-4");
938                 }
939 #endif
940         }
941         
942         // Helper class
943         class RefOnlyMethodClass 
944         {
945                 // Helper static method
946                 static void RefOnlyMethod ()
947                 {
948                 }
949         }
950
951         public class MethodHandleTest<T>
952         {
953                 private List<T> _myList = new List<T> ();
954
955                 public MethodHandleTest ()
956                 {
957                         _myList.Add (default (T));
958                 }
959
960                 public List<T> MyList {
961                         get { return _myList; }
962                         set { _myList = value; }
963                 }
964         }
965 }