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