2 // System.Reflection.MethodInfo Test Cases
5 // Zoltan Varga (vargaz@gmail.com)
7 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
32 using System.Threading;
33 using System.Reflection;
35 using System.Reflection.Emit;
37 using System.Runtime.InteropServices;
38 using System.Runtime.CompilerServices;
40 using System.Collections.Generic;
43 // Disable expected warning
44 #pragma warning disable 169
45 public struct MethodInfoTestStruct {
48 #pragma warning restore 169
50 namespace MonoTests.System.Reflection
53 public class MethodInfoTest
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 ()
62 [MethodImplAttribute(MethodImplOptions.Synchronized)]
63 public void synchronizedMethod ()
68 public void IsDefined_AttributeType_Null ()
70 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
73 mi.IsDefined ((Type) null, false);
75 } catch (ArgumentNullException ex) {
76 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
77 Assert.IsNull (ex.InnerException, "#3");
78 Assert.IsNotNull (ex.Message, "#4");
79 Assert.IsNotNull (ex.ParamName, "#5");
80 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
85 public void TestInvokeByRefReturnMethod ()
88 MethodInfo m = typeof (int[]).GetMethod ("Address");
89 m.Invoke (new int[1], new object[] { 0 });
91 } catch (NotSupportedException e) {
92 Assert.AreEqual (typeof (NotSupportedException), e.GetType (), "#2");
93 Assert.IsNull (e.InnerException, "#3");
94 Assert.IsNotNull (e.Message, "#4");
99 public void PseudoCustomAttributes ()
101 Type t = typeof (MethodInfoTest);
103 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
105 Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
106 Assert.AreEqual ("foo", attr.EntryPoint, "#2");
107 Assert.AreEqual ("libfoo", attr.Value, "#3");
108 Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
109 Assert.AreEqual (false, attr.ExactSpelling, "#5");
110 Assert.AreEqual (true, attr.PreserveSig, "#6");
111 Assert.AreEqual (true, attr.SetLastError, "#7");
112 Assert.AreEqual (true, attr.BestFitMapping, "#8");
113 Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
115 PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
117 // This doesn't work under MS.NET
119 MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
123 [return: MarshalAs (UnmanagedType.Interface)]
124 public void ReturnTypeMarshalAs ()
129 public void ReturnTypePseudoCustomAttributes ()
131 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
133 Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
136 public static int foo (int i, int j)
142 public void StaticInvokeWithObject ()
144 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
146 mi.Invoke (new Object (), new object [] { 1, 2 });
150 public void ByRefInvoke ()
152 MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
153 object[] parms = new object[] {1};
154 met.Invoke (null, parms);
155 Assert.AreEqual (2, parms[0]);
158 public static void ByRefTest (ref int a1)
164 static int byref_arg;
166 public static void ByrefVtype (ref int i) {
173 [Category ("NotDotNet")] // #A2 fails on MS.NET 1.x
175 public void ByrefVtypeInvoke ()
177 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
180 object[] args = new object [] { o };
181 mi.Invoke (null, args);
182 Assert.AreEqual (1, byref_arg, "#A1");
183 Assert.AreEqual (1, o, "#A2");
184 Assert.AreEqual (5, args[0], "#A3");
187 mi.Invoke (null, args);
188 Assert.AreEqual (0, byref_arg, "#B1");
189 Assert.AreEqual (5, args[0], "#B2");
192 public void HeyHey (out string out1, ref DateTime ref1)
197 public void SignatureTest (__arglist)
201 public static unsafe int* PtrFunc (int* a)
207 public void InvokeThreadAbort ()
209 MethodInfo method = typeof (MethodInfoTest).GetMethod ("AbortIt");
211 method.Invoke (null, new object [0]);
214 catch (ThreadAbortException ex) {
215 Thread.ResetAbort ();
216 Assert.IsNull (ex.InnerException, "#2");
220 public static void AbortIt ()
222 Thread.CurrentThread.Abort ();
226 public void ToStringByRef ()
228 Assert.AreEqual ("Void HeyHey(System.String ByRef, System.DateTime ByRef)",
229 this.GetType ().GetMethod ("HeyHey").ToString ());
233 public void ToStringArgList ()
235 Assert.AreEqual ("Void SignatureTest(...)",
236 this.GetType ().GetMethod ("SignatureTest").ToString ());
240 public void ToStringWithPointerSignatures () //bug #409583
242 Assert.AreEqual ("Int32* PtrFunc(Int32*)", this.GetType ().GetMethod ("PtrFunc").ToString ());
245 public struct SimpleStruct
250 public static unsafe SimpleStruct* PtrFunc2 (SimpleStruct* a, A.B.C.MethodInfoTestStruct *b)
252 return (SimpleStruct*) 0;
256 public void ToStringWithPointerSignaturesToNonPrimitiveType ()
258 Assert.AreEqual ("SimpleStruct* PtrFunc2(SimpleStruct*, A.B.C.MethodInfoTestStruct*)",
259 this.GetType ().GetMethod ("PtrFunc2").ToString ());
262 public void ToStringGenericMethod ()
264 Assert.AreEqual ("System.Collections.ObjectModel.ReadOnlyCollection`1[T] AsReadOnly[T](T[])",
265 typeof (Array).GetMethod ("AsReadOnly").ToString ());
268 class GBD_A { public virtual void f () {} }
269 class GBD_B : GBD_A { public override void f () {} }
270 class GBD_C : GBD_B { public override void f () {} }
271 class GBD_D : GBD_C { public new virtual void f () {} }
272 class GBD_E : GBD_D { public override void f () {} }
275 public void GetBaseDefinition ()
277 Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
278 Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
279 Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
282 class TestInheritedMethodA {
283 private void TestMethod ()
287 public void TestMethod2 ()
292 class TestInheritedMethodB : TestInheritedMethodA {
296 public void InheritanceTestGetMethodTest ()
298 MethodInfo inheritedMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
299 MethodInfo baseMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
300 Assert.AreSame (inheritedMethod, baseMethod);
302 MethodInfo inheritedMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
303 MethodInfo baseMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
304 Assert.AreSame (inheritedMethod, baseMethod);
308 public void GetMethodBody_Abstract ()
310 MethodBody mb = typeof (ICloneable).GetMethod ("Clone").GetMethodBody ();
315 public void GetMethodBody_Runtime ()
317 MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
322 public void GetMethodBody_Pinvoke ()
324 MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
329 public void GetMethodBody_Icall ()
331 foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
332 if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
333 MethodBody mb = mi.GetMethodBody ();
338 public static void locals_method ()
340 byte[] b = new byte [10];
343 /* This generates a pinned local */
344 fixed (byte *p = &b [0]) {
350 public void GetMethodBody ()
352 #if MONOTOUCH && !DEBUG
353 Assert.Ignore ("Release app (on devices) are stripped of (managed) IL so this test would fail");
355 MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
357 Assert.IsTrue (mb.InitLocals, "#1");
358 Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
360 IList<LocalVariableInfo> locals = mb.LocalVariables;
362 // This might break with different compilers etc.
363 Assert.AreEqual (2, locals.Count, "#3");
365 Assert.IsTrue ((locals [0].LocalType == typeof (byte[])) || (locals [1].LocalType == typeof (byte[])), "#4");
366 if (locals [0].LocalType == typeof (byte[]))
367 Assert.AreEqual (false, locals [0].IsPinned, "#5");
369 Assert.AreEqual (false, locals [1].IsPinned, "#6");
372 public int return_parameter_test ()
378 public void GetMethodFromHandle_Generic ()
380 MethodHandleTest<int> test = new MethodHandleTest<int> ();
381 RuntimeMethodHandle mh = test.GetType ().GetProperty ("MyList")
382 .GetGetMethod ().MethodHandle;
383 MethodBase mb = MethodInfo.GetMethodFromHandle (mh,
384 typeof (MethodHandleTest<int>).TypeHandle);
385 Assert.IsNotNull (mb, "#1");
386 List<int> list = (List<int>) mb.Invoke (test, null);
387 Assert.IsNotNull (list, "#2");
388 Assert.AreEqual (1, list.Count, "#3");
392 public void ReturnParameter ()
394 ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
395 Assert.AreEqual (typeof (int), pi.ParameterType, "#1");
396 Assert.AreEqual (-1, pi.Position, "#2");
397 // MS always return false here
398 //Assert.IsTrue (pi.IsRetval, "#3");
402 public void InvokeOnRefOnlyAssembly ()
404 Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
405 Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
406 MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
408 m.Invoke (null, new object [0]);
410 } catch (InvalidOperationException ex) {
411 // The requested operation is invalid in the
412 // ReflectionOnly context
413 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
414 Assert.IsNull (ex.InnerException, "#3");
415 Assert.IsNotNull (ex.Message, "#4");
420 [ExpectedException (typeof (TargetInvocationException))]
421 public void InvokeInvalidOpExceptionThrow () {
422 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ThrowMethod");
423 mi.Invoke(null, null);
426 public static void ThrowMethod () {
427 throw new InvalidOperationException ();
431 public void InvokeGenericVtype ()
433 KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
434 Type type = kvp.GetType ();
435 Type [] arguments = type.GetGenericArguments ();
436 MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
437 MethodInfo generic_method = method.MakeGenericMethod (arguments);
438 kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
440 Assert.AreEqual ("a", kvp.Key, "#1");
441 Assert.AreEqual (21, kvp.Value, "#2");
444 public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
450 public void InvokeGenericInst ()
452 List<string> str = null;
454 object [] methodArgs = new object [] { str };
455 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("GenericRefMethod");
456 mi.Invoke (null, methodArgs);
457 Assert.IsNotNull (methodArgs [0], "#A1");
458 Assert.IsNull (str, "#A2");
459 Assert.IsTrue (methodArgs [0] is List<string>, "#A3");
461 List<string> refStr = methodArgs [0] as List<string>;
462 Assert.IsNotNull (refStr, "#B1");
463 Assert.AreEqual (1, refStr.Count, "#B2");
464 Assert.AreEqual ("test", refStr [0], "#B3");
467 public static void GenericRefMethod (ref List<string> strArg)
469 strArg = new List<string> ();
473 public void MakeGenericMethodArgsMismatchFoo<T> ()
478 public void MakeGenericMethodArgsMismatch ()
480 MethodInfo gmi = this.GetType ().GetMethod (
481 "MakeGenericMethodArgsMismatchFoo");
483 gmi.MakeGenericMethod ();
485 } catch (ArgumentException ex) {
486 // The type or method has 1 generic parameter(s),
487 // but 0 generic argument(s) were provided. A
488 // generic argument must be provided for each
490 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
491 Assert.IsNull (ex.InnerException, "#3");
492 Assert.IsNotNull (ex.Message, "#4");
493 Assert.IsNull (ex.ParamName, "#5");
497 public void SimpleGenericMethod<TFoo, TBar> () {}
500 public void MakeGenericMethodWithNullArray ()
502 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
504 gmi.MakeGenericMethod ((Type []) null);
506 } catch (ArgumentNullException ex) {
507 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
508 Assert.IsNull (ex.InnerException, "#3");
509 Assert.IsNotNull (ex.Message, "#4");
510 Assert.AreEqual ("methodInstantiation", ex.ParamName, "#5");
515 public void MakeGenericMethodWithNullValueInTypesArray ()
517 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
519 gmi.MakeGenericMethod (new Type [] { typeof (int), null });
521 } catch (ArgumentNullException ex) {
522 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
523 Assert.IsNull (ex.InnerException, "#3");
524 Assert.IsNotNull (ex.Message, "#4");
525 Assert.IsNull (ex.ParamName, "#5");
530 public void MakeGenericMethodWithNonGenericMethodDefinitionMethod ()
532 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
533 MethodInfo inst = gmi.MakeGenericMethod (typeof (int), typeof (double));
535 inst.MakeGenericMethod (typeof (int), typeof (double));
537 } catch (InvalidOperationException ex) {
541 public TFoo SimpleGenericMethod2<TFoo, TBar> () { return default (TFoo); }
542 /*Test for the uggly broken behavior of SRE.*/
544 public void MakeGenericMethodWithSreTypeResultsInStupidMethodInfo ()
546 AssemblyName assemblyName = new AssemblyName ();
547 assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodInfoTest";
548 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
549 ModuleBuilder module = assembly.DefineDynamicModule ("module1", "tst.dll");
550 TypeBuilder tb = module.DefineType ("Test", TypeAttributes.Public);
552 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod2");
553 MethodInfo ins = gmi.MakeGenericMethod (typeof (int), tb);
555 Assert.AreSame (tb, ins.GetGenericArguments () [1], "#1");
556 /*broken ReturnType*/
557 Assert.AreSame (gmi.GetGenericArguments () [0], ins.ReturnType, "#2");
560 public static int? pass_nullable (int? i)
566 public void NullableTests ()
568 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
569 Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
570 Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
572 // Test conversion of vtype to a nullable type for the this argument
573 PropertyInfo pi = typeof (Nullable <int>).GetProperty ("HasValue");
574 Assert.AreEqual (true, pi.GetGetMethod ().Invoke (10, null));
575 PropertyInfo pi2 = typeof (Nullable <int>).GetProperty ("Value");
576 Assert.AreEqual (10, pi2.GetGetMethod ().Invoke (10, null));
579 public static void foo_generic<T> ()
584 public void IsGenericMethod ()
586 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
587 Assert.AreEqual (true, mi.IsGenericMethod, "#1");
588 MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
589 Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
590 MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
591 Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
596 public static void Foo<T2> (T2 i)
600 public static void Bar ()
606 public static void Baz ()
613 public void ContainsGenericParameters ()
615 // Non-generic method in open generic type
616 Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
617 // open generic method in closed generic type
618 Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
619 // non-generic method in closed generic type
620 Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
621 // closed generic method in closed generic type
622 Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
623 // non-generic method in non-generic nested type of closed generic type
624 Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
625 // non-generic method in non-generic nested type of open generic type
626 Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
630 public void IsGenericMethodDefinition ()
632 MethodInfo m1 = typeof (A<>).GetMethod ("Foo");
633 Assert.IsTrue (m1.IsGenericMethod, "#A1");
634 Assert.IsTrue (m1.IsGenericMethodDefinition, "#A2");
636 MethodInfo m2 = typeof (A<int>).GetMethod ("Foo");
637 Assert.IsTrue (m2.IsGenericMethod, "#B1");
638 Assert.IsTrue (m2.IsGenericMethodDefinition, "#B2");
640 MethodInfo m3 = m2.MakeGenericMethod (typeof (int));
641 Assert.IsTrue (m3.IsGenericMethod, "#C1");
642 Assert.IsFalse (m3.IsGenericMethodDefinition, "#C2");
646 public void GetGenericMethodDefinition ()
648 MethodInfo mi1 = typeof (MyList<>).GetMethod ("ConvertAll");
649 MethodInfo mi2 = typeof (MyList<int>).GetMethod ("ConvertAll");
651 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[T,TOutput]",
652 mi1.GetParameters () [0].ParameterType.ToString (), "#A1");
653 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[System.Int32,TOutput]",
654 mi2.GetParameters () [0].ParameterType.ToString (), "#A2");
655 Assert.IsTrue (mi1.IsGenericMethod, "#A3");
656 Assert.IsTrue (mi1.IsGenericMethodDefinition, "#A4");
657 Assert.IsTrue (mi2.IsGenericMethod, "#A5");
658 Assert.IsTrue (mi2.IsGenericMethodDefinition, "#A6");
660 MethodInfo mi3 = mi2.GetGenericMethodDefinition ();
662 Assert.IsTrue (mi3.IsGenericMethod, "#B1");
663 Assert.IsTrue (mi3.IsGenericMethodDefinition, "#B2");
664 Assert.AreSame (mi2, mi3, "#B3");
666 MethodInfo mi4 = mi2.MakeGenericMethod (typeof (short));
667 Assert.IsTrue (mi4.IsGenericMethod, "#C1");
668 Assert.IsFalse (mi4.IsGenericMethodDefinition, "#C2");
669 Assert.AreSame (mi2, mi4.GetGenericMethodDefinition (), "#C3");
672 public void TestMethod123(int a, int b) {}
675 public void GetParametersDontReturnInternedArray ()
677 var method = typeof (MethodInfoTest).GetMethod ("TestMethod123");
678 var parms = method.GetParameters ();
679 Assert.AreNotSame (parms, method.GetParameters (), "#1");
682 Assert.IsNotNull (method.GetParameters () [0], "#2");
686 public void Bug354757 ()
688 MethodInfo gmd = (typeof (MyList <int>)).GetMethod ("ConvertAll");
689 MethodInfo oi = gmd.MakeGenericMethod (gmd.GetGenericArguments ());
690 Assert.AreSame (gmd, oi);
694 [ExpectedException (typeof (ArgumentException))]
696 [Category ("NotWorking")] // #10552
698 public void MakeGenericMethodRespectConstraints ()
700 var m = typeof (MethodInfoTest).GetMethod ("TestMethod");
701 m.MakeGenericMethod (typeof (Type));
704 public void TestMethod <T> () where T : Exception
708 public class MyList<T>
710 public TOutput ConvertAll<TOutput> (Foo<T,TOutput> arg)
712 return default (TOutput);
714 public T ConvertAll2 (MyList<T> arg)
720 public class Foo<T,TOutput>
724 class GenericHelper<T>
726 public void Test (T t)
731 interface IMethodInvoke<out T>
736 class MethodInvoke : IMethodInvoke<string>
738 public string Test ()
740 return "MethodInvoke";
745 public void GetInterfaceMapWorksWithVariantIfaces ()
747 var m0 = typeof (IMethodInvoke<object>).GetMethod ("Test");
748 var m1 = typeof (IMethodInvoke<string>).GetMethod ("Test");
749 var obj = new MethodInvoke ();
751 Assert.AreEqual ("MethodInvoke", m0.Invoke (obj, new Object [0]));
752 Assert.AreEqual ("MethodInvoke", m1.Invoke (obj, new Object [0]));
757 public int? Bug12856 ()
763 public void MethodToStringShouldPrintFullNameOfGenericStructs ()
765 var m = GetType ().GetMethod ("Bug12856");
766 Assert.AreEqual ("System.Nullable`1[System.Int32] Bug12856()", m.ToString (), "#1");
770 class GenericClass<T>
772 public void Method ()
775 Console.WriteLine(lv);
778 public void Method2<K> (T a0, K a1)
782 Console.WriteLine (var0);
783 Console.WriteLine (var1);
788 public void TestLocalVariableTypes ()
790 var typeofT = typeof (GenericClass<>).GetGenericArguments () [0];
791 var typeofK = typeof (GenericClass<>).GetMethod ("Method2").GetGenericArguments () [0];
793 var type = typeof (GenericClass<>).GetMethod("Method").GetMethodBody().LocalVariables[0].LocalType;
794 Assert.AreEqual (typeofT, type);
795 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
797 type = typeof (GenericClass<>).GetMethod("Method2").GetMethodBody().LocalVariables[0].LocalType;
798 Assert.AreEqual (typeofT, type);
799 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
801 type = typeof (GenericClass<>).GetMethod("Method2").GetMethodBody().LocalVariables[1].LocalType;
802 Assert.AreEqual (typeofK, type);
803 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
805 type = typeof (GenericClass<int>).GetMethod("Method2").GetMethodBody().LocalVariables[0].LocalType;
806 Assert.AreEqual (typeof (int), type);
808 type = typeof (GenericClass<int>).GetMethod("Method2").GetMethodBody().LocalVariables[1].LocalType;
809 Assert.AreEqual (typeofK, type);
810 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
816 class RefOnlyMethodClass
818 // Helper static method
819 static void RefOnlyMethod ()
824 public class MethodHandleTest<T>
826 private List<T> _myList = new List<T> ();
828 public MethodHandleTest ()
830 _myList.Add (default (T));
833 public List<T> MyList {
834 get { return _myList; }
835 set { _myList = value; }