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