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