2 // System.Reflection.MethodInfo Test Cases
5 // Zoltan Varga (vargaz@gmail.com)
6 // Aleksey Kliger (aleksey@xamarin.com)
8 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2015 Xamarin, Inc. (http://www.xamarin.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
34 using System.Threading;
35 using System.Reflection;
36 #if !MONOTOUCH && !FULL_AOT_RUNTIME
37 using System.Reflection.Emit;
39 using System.Runtime.InteropServices;
40 using System.Runtime.CompilerServices;
42 using System.Collections.Generic;
45 // Disable expected warning
46 #pragma warning disable 169
47 public struct MethodInfoTestStruct {
50 #pragma warning restore 169
52 namespace MonoTests.System.Reflection
55 public class MethodInfoTest
57 #if MONOTOUCH || FULL_AOT_RUNTIME
58 // use an existing symbol - so we can build without dlsym. It does not matter that the signature does not match for the test
59 [DllImport ("libc", EntryPoint="readlink", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
61 [DllImport ("libfoo", EntryPoint="foo", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
63 public static extern void dllImportMethod ();
64 [MethodImplAttribute(MethodImplOptions.PreserveSig)]
65 public void preserveSigMethod ()
69 [MethodImplAttribute(MethodImplOptions.Synchronized)]
70 public void synchronizedMethod ()
74 public interface InterfaceTest
80 public void IsDefined_AttributeType_Null ()
82 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
85 mi.IsDefined ((Type) null, false);
87 } catch (ArgumentNullException ex) {
88 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
89 Assert.IsNull (ex.InnerException, "#3");
90 Assert.IsNotNull (ex.Message, "#4");
91 Assert.IsNotNull (ex.ParamName, "#5");
92 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
97 public void TestInvokeByRefReturnMethod ()
100 MethodInfo m = typeof (int[]).GetMethod ("Address");
101 m.Invoke (new int[1], new object[] { 0 });
103 } catch (NotSupportedException e) {
104 Assert.AreEqual (typeof (NotSupportedException), e.GetType (), "#2");
105 Assert.IsNull (e.InnerException, "#3");
106 Assert.IsNotNull (e.Message, "#4");
111 public void PseudoCustomAttributes ()
113 Type t = typeof (MethodInfoTest);
115 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
117 Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
118 #if MONOTOUCH || FULL_AOT_RUNTIME
119 Assert.AreEqual ("readlink", attr.EntryPoint, "#2");
120 Assert.AreEqual ("libc", attr.Value, "#3");
122 Assert.AreEqual ("foo", attr.EntryPoint, "#2");
123 Assert.AreEqual ("libfoo", attr.Value, "#3");
125 Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
126 Assert.AreEqual (false, attr.ExactSpelling, "#5");
127 Assert.AreEqual (true, attr.PreserveSig, "#6");
128 Assert.AreEqual (true, attr.SetLastError, "#7");
129 Assert.AreEqual (true, attr.BestFitMapping, "#8");
130 Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
132 PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
134 // This doesn't work under MS.NET
136 MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
140 [return: MarshalAs (UnmanagedType.Interface)]
141 public void ReturnTypeMarshalAs ()
146 public void ReturnTypePseudoCustomAttributes ()
148 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
150 Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
153 public static int foo (int i, int j)
159 public void StaticInvokeWithObject ()
161 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
163 mi.Invoke (new Object (), new object [] { 1, 2 });
167 public void ByRefInvoke ()
169 MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
170 object[] parms = new object[] {1};
171 met.Invoke (null, parms);
172 Assert.AreEqual (2, parms[0]);
175 public static void ByRefTest (ref int a1)
181 static int byref_arg;
183 public static void ByrefVtype (ref int i) {
189 public void ByrefVtypeInvoke ()
191 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
194 object[] args = new object [] { o };
195 mi.Invoke (null, args);
196 Assert.AreEqual (1, byref_arg, "#A1");
197 Assert.AreEqual (1, o, "#A2");
198 Assert.AreEqual (5, args[0], "#A3");
201 mi.Invoke (null, args);
202 Assert.AreEqual (0, byref_arg, "#B1");
203 Assert.AreEqual (5, args[0], "#B2");
206 public void HeyHey (out string out1, ref DateTime ref1)
211 public void SignatureTest (__arglist)
215 public static unsafe int* PtrFunc (int* a)
220 #if MONO_FEATURE_THREAD_ABORT
222 public void InvokeThreadAbort ()
224 MethodInfo method = typeof (MethodInfoTest).GetMethod ("AbortIt");
226 method.Invoke (null, new object [0]);
229 catch (ThreadAbortException ex) {
230 Thread.ResetAbort ();
231 Assert.IsNull (ex.InnerException, "#2");
235 public static void AbortIt ()
237 Thread.CurrentThread.Abort ();
242 public void ToStringByRef ()
244 Assert.AreEqual ("Void HeyHey(System.String ByRef, System.DateTime ByRef)",
245 this.GetType ().GetMethod ("HeyHey").ToString ());
249 public void ToStringArgList ()
251 Assert.AreEqual ("Void SignatureTest(...)",
252 this.GetType ().GetMethod ("SignatureTest").ToString ());
256 public void ToStringWithPointerSignatures () //bug #409583
258 Assert.AreEqual ("Int32* PtrFunc(Int32*)", this.GetType ().GetMethod ("PtrFunc").ToString ());
261 public struct SimpleStruct
266 public static unsafe SimpleStruct* PtrFunc2 (SimpleStruct* a, A.B.C.MethodInfoTestStruct *b)
268 return (SimpleStruct*) 0;
272 public void ToStringWithPointerSignaturesToNonPrimitiveType ()
274 Assert.AreEqual ("SimpleStruct* PtrFunc2(SimpleStruct*, A.B.C.MethodInfoTestStruct*)",
275 this.GetType ().GetMethod ("PtrFunc2").ToString ());
278 public void ToStringGenericMethod ()
280 Assert.AreEqual ("System.Collections.ObjectModel.ReadOnlyCollection`1[T] AsReadOnly[T](T[])",
281 typeof (Array).GetMethod ("AsReadOnly").ToString ());
284 class GBD_A { public virtual void f () {} }
285 class GBD_B : GBD_A { public override void f () {} }
286 class GBD_C : GBD_B { public override void f () {} }
287 class GBD_D : GBD_C { public new virtual void f () {} }
288 class GBD_E : GBD_D { public override void f () {} }
291 public void GetBaseDefinition ()
293 Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
294 Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
295 Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
298 class GenericBase<T,H> {
299 public virtual void f2 () { }
302 class GenericMid<T, U> : GenericBase<T, Action<U>> {
303 public virtual T f1 () { return default (T); }
306 class GenericChild<T> : GenericMid<T, int> {
307 public override T f1 () { return default (T); }
308 public override void f2 () { }
311 class DerivedFromGenericBase : GenericBase<int, int> {
315 public void GetBaseDefinition_OpenConstructedBaseType () // 36305
317 var t = typeof (GenericChild<string>);
319 var mi1 = t.GetMethod ("f1");
320 var mi1_base = mi1.GetBaseDefinition ();
322 Assert.AreEqual (typeof (GenericMid<string, int>), mi1_base.DeclaringType, "#1");
324 var mi2 = t.GetMethod ("f2");
325 var mi2_base = mi2.GetBaseDefinition ();
327 Assert.AreEqual (typeof (GenericBase<string, Action<int>>), mi2_base.DeclaringType, "#2");
330 class TestInheritedMethodA {
331 private void TestMethod ()
335 public void TestMethod2 ()
340 class TestInheritedMethodB : TestInheritedMethodA {
344 public void InheritanceTestGetMethodTest ()
346 MethodInfo inheritedMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
347 MethodInfo baseMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
348 Assert.AreSame (inheritedMethod, baseMethod);
350 MethodInfo inheritedMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
351 MethodInfo baseMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
352 Assert.AreSame (inheritedMethod, baseMethod);
356 public void GetMethodBody_Abstract ()
358 MethodBody mb = typeof (InterfaceTest).GetMethod ("Clone").GetMethodBody ();
363 public void GetMethodBody_Runtime ()
365 MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
370 public void GetMethodBody_Pinvoke ()
372 MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
377 public void GetMethodBody_Icall ()
379 foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
380 if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
381 MethodBody mb = mi.GetMethodBody ();
386 public static void locals_method ()
388 byte[] b = new byte [10];
391 /* This generates a pinned local */
392 fixed (byte *p = &b [0]) {
398 public void GetMethodBody ()
400 #if (MONOTOUCH || FULL_AOT_RUNTIME) && !DEBUG
401 Assert.Ignore ("Release app (on devices) are stripped of (managed) IL so this test would fail");
403 MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
405 Assert.IsTrue (mb.InitLocals, "#1");
406 Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
408 IList<LocalVariableInfo> locals = mb.LocalVariables;
410 bool foundPinnedBytePointer = false;
412 foreach (LocalVariableInfo lvi in locals) {
413 if (lvi.LocalType == typeof (byte[]))
414 // This is optimized out by CSC in .NET 4.6
415 Assert.IsFalse (lvi.IsPinned, "#3-1");
417 if (/* mcs */ lvi.LocalType == typeof (byte*) || /* csc */ lvi.LocalType == typeof (byte).MakeByRefType ()) {
418 foundPinnedBytePointer = true;
419 Assert.IsTrue (lvi.IsPinned, "#3-2");
423 Assert.IsTrue (foundPinnedBytePointer, "#4");
426 public int return_parameter_test ()
432 public void GetMethodFromHandle_Generic ()
434 MethodHandleTest<int> test = new MethodHandleTest<int> ();
435 RuntimeMethodHandle mh = test.GetType ().GetProperty ("MyList")
436 .GetGetMethod ().MethodHandle;
437 MethodBase mb = MethodInfo.GetMethodFromHandle (mh,
438 typeof (MethodHandleTest<int>).TypeHandle);
439 Assert.IsNotNull (mb, "#1");
440 List<int> list = (List<int>) mb.Invoke (test, null);
441 Assert.IsNotNull (list, "#2");
442 Assert.AreEqual (1, list.Count, "#3");
446 public void ReturnParameter ()
448 ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
449 Assert.AreEqual (typeof (int), pi.ParameterType, "#1");
450 Assert.AreEqual (-1, pi.Position, "#2");
451 // MS always return false here
452 //Assert.IsTrue (pi.IsRetval, "#3");
456 public void MethodInfoModule ()
458 Type type = typeof (MethodInfoTest);
459 MethodInfo me = type.GetMethod ("return_parameter_test");
461 Assert.AreEqual (type.Module, me.Module);
465 public void InvokeOnRefOnlyAssembly ()
467 Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
468 Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
469 MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
471 m.Invoke (null, new object [0]);
473 } catch (InvalidOperationException ex) {
474 // The requested operation is invalid in the
475 // ReflectionOnly context
476 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
477 Assert.IsNull (ex.InnerException, "#3");
478 Assert.IsNotNull (ex.Message, "#4");
483 [ExpectedException (typeof (TargetInvocationException))]
484 public void InvokeInvalidOpExceptionThrow () {
485 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ThrowMethod");
486 mi.Invoke(null, null);
489 public static void ThrowMethod () {
490 throw new InvalidOperationException ();
494 public void InvokeGenericVtype ()
496 KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
497 Type type = kvp.GetType ();
498 Type [] arguments = type.GetGenericArguments ();
499 MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
500 MethodInfo generic_method = method.MakeGenericMethod (arguments);
501 kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
503 Assert.AreEqual ("a", kvp.Key, "#1");
504 Assert.AreEqual (21, kvp.Value, "#2");
507 public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
513 public void InvokeGenericInst ()
515 List<string> str = null;
517 object [] methodArgs = new object [] { str };
518 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("GenericRefMethod");
519 mi.Invoke (null, methodArgs);
520 Assert.IsNotNull (methodArgs [0], "#A1");
521 Assert.IsNull (str, "#A2");
522 Assert.IsTrue (methodArgs [0] is List<string>, "#A3");
524 List<string> refStr = methodArgs [0] as List<string>;
525 Assert.IsNotNull (refStr, "#B1");
526 Assert.AreEqual (1, refStr.Count, "#B2");
527 Assert.AreEqual ("test", refStr [0], "#B3");
530 public static void GenericRefMethod (ref List<string> strArg)
532 strArg = new List<string> ();
536 public void MakeGenericMethodArgsMismatchFoo<T> ()
541 public void MakeGenericMethodArgsMismatch ()
543 MethodInfo gmi = this.GetType ().GetMethod (
544 "MakeGenericMethodArgsMismatchFoo");
546 gmi.MakeGenericMethod ();
548 } catch (ArgumentException ex) {
549 // The type or method has 1 generic parameter(s),
550 // but 0 generic argument(s) were provided. A
551 // generic argument must be provided for each
553 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
554 Assert.IsNull (ex.InnerException, "#3");
555 Assert.IsNotNull (ex.Message, "#4");
556 Assert.IsNull (ex.ParamName, "#5");
560 public void SimpleGenericMethod<TFoo, TBar> () {}
563 public void MakeGenericMethodWithNullArray ()
565 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
567 gmi.MakeGenericMethod ((Type []) null);
569 } catch (ArgumentNullException ex) {
570 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
571 Assert.IsNull (ex.InnerException, "#3");
572 Assert.IsNotNull (ex.Message, "#4");
573 Assert.AreEqual ("methodInstantiation", ex.ParamName, "#5");
578 public void MakeGenericMethodWithNullValueInTypesArray ()
580 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
582 gmi.MakeGenericMethod (new Type [] { typeof (int), null });
584 } catch (ArgumentNullException ex) {
585 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
586 Assert.IsNull (ex.InnerException, "#3");
587 Assert.IsNotNull (ex.Message, "#4");
588 Assert.IsNull (ex.ParamName, "#5");
593 public void MakeGenericMethodWithNonGenericMethodDefinitionMethod ()
595 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
596 MethodInfo inst = gmi.MakeGenericMethod (typeof (int), typeof (double));
598 inst.MakeGenericMethod (typeof (int), typeof (double));
600 } catch (InvalidOperationException ex) {
603 #if !MONOTOUCH && !FULL_AOT_RUNTIME
604 public TFoo SimpleGenericMethod2<TFoo, TBar> () { return default (TFoo); }
605 /*Test for the uggly broken behavior of SRE.*/
607 public void MakeGenericMethodWithSreTypeResultsInStupidMethodInfo ()
609 AssemblyName assemblyName = new AssemblyName ();
610 assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodInfoTest";
611 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
612 ModuleBuilder module = assembly.DefineDynamicModule ("module1", "tst.dll");
613 TypeBuilder tb = module.DefineType ("Test", TypeAttributes.Public);
615 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod2");
616 MethodInfo ins = gmi.MakeGenericMethod (typeof (int), tb);
618 Assert.AreSame (tb, ins.GetGenericArguments () [1], "#1");
619 /*broken ReturnType*/
620 Assert.AreSame (gmi.GetGenericArguments () [0], ins.ReturnType, "#2");
623 public static int? pass_nullable (int? i)
629 public void NullableTests ()
631 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
632 Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
633 Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
635 // Test conversion of vtype to a nullable type for the this argument
636 PropertyInfo pi = typeof (Nullable <int>).GetProperty ("HasValue");
637 Assert.AreEqual (true, pi.GetGetMethod ().Invoke (10, null));
638 PropertyInfo pi2 = typeof (Nullable <int>).GetProperty ("Value");
639 Assert.AreEqual (10, pi2.GetGetMethod ().Invoke (10, null));
642 public static void foo_generic<T> ()
647 public void IsGenericMethod ()
649 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
650 Assert.AreEqual (true, mi.IsGenericMethod, "#1");
651 MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
652 Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
653 MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
654 Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
659 public static void Foo<T2> (T2 i)
663 public static void Bar ()
669 public static void Baz ()
676 public void ContainsGenericParameters ()
678 // Non-generic method in open generic type
679 Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
680 // open generic method in closed generic type
681 Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
682 // non-generic method in closed generic type
683 Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
684 // closed generic method in closed generic type
685 Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
686 // non-generic method in non-generic nested type of closed generic type
687 Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
688 // non-generic method in non-generic nested type of open generic type
689 Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
693 public void IsGenericMethodDefinition ()
695 MethodInfo m1 = typeof (A<>).GetMethod ("Foo");
696 Assert.IsTrue (m1.IsGenericMethod, "#A1");
697 Assert.IsTrue (m1.IsGenericMethodDefinition, "#A2");
699 MethodInfo m2 = typeof (A<int>).GetMethod ("Foo");
700 Assert.IsTrue (m2.IsGenericMethod, "#B1");
701 Assert.IsTrue (m2.IsGenericMethodDefinition, "#B2");
703 MethodInfo m3 = m2.MakeGenericMethod (typeof (int));
704 Assert.IsTrue (m3.IsGenericMethod, "#C1");
705 Assert.IsFalse (m3.IsGenericMethodDefinition, "#C2");
709 public void GetGenericMethodDefinition ()
711 MethodInfo mi1 = typeof (MyList<>).GetMethod ("ConvertAll");
712 MethodInfo mi2 = typeof (MyList<int>).GetMethod ("ConvertAll");
714 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[T,TOutput]",
715 mi1.GetParameters () [0].ParameterType.ToString (), "#A1");
716 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[System.Int32,TOutput]",
717 mi2.GetParameters () [0].ParameterType.ToString (), "#A2");
718 Assert.IsTrue (mi1.IsGenericMethod, "#A3");
719 Assert.IsTrue (mi1.IsGenericMethodDefinition, "#A4");
720 Assert.IsTrue (mi2.IsGenericMethod, "#A5");
721 Assert.IsTrue (mi2.IsGenericMethodDefinition, "#A6");
723 MethodInfo mi3 = mi2.GetGenericMethodDefinition ();
725 Assert.IsTrue (mi3.IsGenericMethod, "#B1");
726 Assert.IsTrue (mi3.IsGenericMethodDefinition, "#B2");
727 Assert.AreSame (mi2, mi3, "#B3");
729 MethodInfo mi4 = mi2.MakeGenericMethod (typeof (short));
730 Assert.IsTrue (mi4.IsGenericMethod, "#C1");
731 Assert.IsFalse (mi4.IsGenericMethodDefinition, "#C2");
732 Assert.AreSame (mi2, mi4.GetGenericMethodDefinition (), "#C3");
735 public void TestMethod123(int a, int b) {}
738 public void GetParametersDontReturnInternedArray ()
740 var method = typeof (MethodInfoTest).GetMethod ("TestMethod123");
741 var parms = method.GetParameters ();
742 Assert.AreNotSame (parms, method.GetParameters (), "#1");
745 Assert.IsNotNull (method.GetParameters () [0], "#2");
749 public void Bug354757 ()
751 MethodInfo gmd = (typeof (MyList <int>)).GetMethod ("ConvertAll");
752 MethodInfo oi = gmd.MakeGenericMethod (gmd.GetGenericArguments ());
753 Assert.AreSame (gmd, oi);
757 [ExpectedException (typeof (ArgumentException))]
759 [Category ("NotWorking")] // #10552
761 public void MakeGenericMethodRespectConstraints ()
763 var m = typeof (MethodInfoTest).GetMethod ("TestMethod");
764 m.MakeGenericMethod (typeof (Type));
767 public void TestMethod <T> () where T : Exception
771 public class MyList<T>
773 public TOutput ConvertAll<TOutput> (Foo<T,TOutput> arg)
775 return default (TOutput);
777 public T ConvertAll2 (MyList<T> arg)
783 public class Foo<T,TOutput>
787 class GenericHelper<T>
789 public void Test (T t)
793 interface IMethodInvoke<out T>
798 class MethodInvoke : IMethodInvoke<string>
800 public string Test ()
802 return "MethodInvoke";
807 public void GetInterfaceMapWorksWithVariantIfaces ()
809 var m0 = typeof (IMethodInvoke<object>).GetMethod ("Test");
810 var m1 = typeof (IMethodInvoke<string>).GetMethod ("Test");
811 var obj = new MethodInvoke ();
813 Assert.AreEqual ("MethodInvoke", m0.Invoke (obj, new Object [0]));
814 Assert.AreEqual ("MethodInvoke", m1.Invoke (obj, new Object [0]));
818 public int? Bug12856 ()
824 public void MethodToStringShouldPrintFullNameOfGenericStructs ()
826 var m = GetType ().GetMethod ("Bug12856");
827 Assert.AreEqual ("System.Nullable`1[System.Int32] Bug12856()", m.ToString (), "#1");
831 public void GetReflectedType () // #12205
833 // public method declared in base type, queried from a derived type
834 MethodInfo mi = typeof (TestInheritedMethodB).GetMethod ("TestMethod2");
835 Assert.AreEqual (mi.ReflectedType, typeof (TestInheritedMethodB), "#1");
837 // public method declared in a generic class,
838 // queried from a non-generic class derived
839 // from an instantiation of the generic class.
840 mi = typeof (DerivedFromGenericBase).GetMethod ("f2");
841 Assert.AreEqual (mi.ReflectedType, typeof (DerivedFromGenericBase), "#2");
843 // public method declared in a generic class,
844 // queried from the generic type defintion of
845 // a generic derived class.
846 mi = typeof (GenericMid<,>).GetMethod ("f2");
847 Assert.AreEqual (mi.ReflectedType, typeof (GenericMid<,>), "#3");
849 // public method declared in a generic class,
850 // queried from an instantiation of a generic
852 mi = typeof (GenericMid<int,int>).GetMethod ("f2");
853 Assert.AreEqual (mi.ReflectedType, typeof (GenericMid<int,int>), "#4");
857 #if !MONOTOUCH && !FULL_AOT_RUNTIME
858 class GenericClass<T>
860 public void Method ()
863 Console.WriteLine(lv);
866 public void Method2<K> (T a0, K a1)
870 Console.WriteLine (var0);
871 Console.WriteLine (var1);
876 public void TestLocalVariableTypes ()
878 var typeofT = typeof (GenericClass<>).GetGenericArguments () [0];
879 var typeofK = typeof (GenericClass<>).GetMethod ("Method2").GetGenericArguments () [0];
881 var type = typeof (GenericClass<>).GetMethod("Method").GetMethodBody().LocalVariables[0].LocalType;
882 Assert.AreEqual (typeofT, type);
883 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
885 bool foundTypeOfK = false;
886 bool foundExpectedType = false;
888 MethodBody mb = typeof (GenericClass<>).GetMethod("Method2").GetMethodBody();
889 foreach (LocalVariableInfo lvi in mb.LocalVariables) {
890 if (lvi.LocalType == typeofK) {
892 Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#1-1");
893 } else if (lvi.LocalType == typeofT) {
894 foundExpectedType = true;
895 Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#1-2");
899 Assert.IsTrue (foundTypeOfK, "#1-3");
900 if (mb.LocalVariables.Count < 2)
901 Assert.Ignore ("Code built in release mode - 'T var0' optmized out");
903 Assert.IsTrue (foundExpectedType, "#1-4");
905 foundTypeOfK = false;
906 foundExpectedType = false;
907 mb = typeof (GenericClass<int>).GetMethod("Method2").GetMethodBody();
908 foreach (LocalVariableInfo lvi in mb.LocalVariables) {
909 if (lvi.LocalType == typeofK) {
911 Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#2-1");
912 } else if (lvi.LocalType == typeof (int)) {
913 foundExpectedType = true;
917 Assert.IsTrue (foundTypeOfK, "#2-3");
918 if (mb.LocalVariables.Count < 2)
919 Assert.Ignore ("Code built in release mode - 'int var0' optmized out");
921 Assert.IsTrue (foundExpectedType, "#2-4");
927 class RefOnlyMethodClass
929 // Helper static method
930 static void RefOnlyMethod ()
935 public class MethodHandleTest<T>
937 private List<T> _myList = new List<T> ();
939 public MethodHandleTest ()
941 _myList.Add (default (T));
944 public List<T> MyList {
945 get { return _myList; }
946 set { _myList = value; }