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