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