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;
41 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 [DllImport ("libfoo", EntryPoint="foo", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
58 public static extern void dllImportMethod ();
59 [MethodImplAttribute(MethodImplOptions.PreserveSig)]
60 public void preserveSigMethod ()
64 [MethodImplAttribute(MethodImplOptions.Synchronized)]
65 public void synchronizedMethod ()
70 public void IsDefined_AttributeType_Null ()
72 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
75 mi.IsDefined ((Type) null, false);
77 } catch (ArgumentNullException ex) {
78 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
79 Assert.IsNull (ex.InnerException, "#3");
80 Assert.IsNotNull (ex.Message, "#4");
81 Assert.IsNotNull (ex.ParamName, "#5");
82 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
87 public void TestInvokeByRefReturnMethod ()
90 MethodInfo m = typeof (int[]).GetMethod ("Address");
91 m.Invoke (new int[1], new object[] { 0 });
93 } catch (NotSupportedException e) {
94 Assert.AreEqual (typeof (NotSupportedException), e.GetType (), "#2");
95 Assert.IsNull (e.InnerException, "#3");
96 Assert.IsNotNull (e.Message, "#4");
102 public void PseudoCustomAttributes ()
104 Type t = typeof (MethodInfoTest);
106 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
108 Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
109 Assert.AreEqual ("foo", attr.EntryPoint, "#2");
110 Assert.AreEqual ("libfoo", attr.Value, "#3");
111 Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
112 Assert.AreEqual (false, attr.ExactSpelling, "#5");
113 Assert.AreEqual (true, attr.PreserveSig, "#6");
114 Assert.AreEqual (true, attr.SetLastError, "#7");
115 Assert.AreEqual (true, attr.BestFitMapping, "#8");
116 Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
118 PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
120 // This doesn't work under MS.NET
122 MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
126 [return: MarshalAs (UnmanagedType.Interface)]
127 public void ReturnTypeMarshalAs ()
132 public void ReturnTypePseudoCustomAttributes ()
134 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
136 Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
140 public static int foo (int i, int j)
146 public void StaticInvokeWithObject ()
148 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
150 mi.Invoke (new Object (), new object [] { 1, 2 });
154 public void ByRefInvoke ()
156 MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
157 object[] parms = new object[] {1};
158 met.Invoke (null, parms);
159 Assert.AreEqual (2, parms[0]);
162 public static void ByRefTest (ref int a1)
168 static int byref_arg;
170 public static void ByrefVtype (ref int i) {
177 [Category ("NotDotNet")] // #A2 fails on MS.NET 1.x
179 public void ByrefVtypeInvoke ()
181 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
184 object[] args = new object [] { o };
185 mi.Invoke (null, args);
186 Assert.AreEqual (1, byref_arg, "#A1");
187 Assert.AreEqual (1, o, "#A2");
188 Assert.AreEqual (5, args[0], "#A3");
191 mi.Invoke (null, args);
192 Assert.AreEqual (0, byref_arg, "#B1");
193 Assert.AreEqual (5, args[0], "#B2");
196 public void HeyHey (out string out1, ref DateTime ref1)
201 public void SignatureTest (__arglist)
205 public static unsafe int* PtrFunc (int* a)
211 public void InvokeThreadAbort ()
213 MethodInfo method = typeof (MethodInfoTest).GetMethod ("AbortIt");
215 method.Invoke (null, new object [0]);
219 catch (ThreadAbortException ex) {
220 Thread.ResetAbort ();
221 Assert.IsNull (ex.InnerException, "#2");
224 catch (TargetInvocationException ex) {
225 Thread.ResetAbort ();
226 Assert.IsNotNull (ex.InnerException, "#2");
227 Assert.AreEqual (typeof (ThreadAbortException), ex.InnerException.GetType (), "#3");
232 public static void AbortIt ()
234 Thread.CurrentThread.Abort ();
238 public void ToStringByRef ()
240 Assert.AreEqual ("Void HeyHey(System.String ByRef, System.DateTime ByRef)",
241 this.GetType ().GetMethod ("HeyHey").ToString ());
245 public void ToStringArgList ()
247 Assert.AreEqual ("Void SignatureTest(...)",
248 this.GetType ().GetMethod ("SignatureTest").ToString ());
252 public void ToStringWithPointerSignatures () //bug #409583
254 Assert.AreEqual ("Int32* PtrFunc(Int32*)", this.GetType ().GetMethod ("PtrFunc").ToString ());
257 public struct SimpleStruct
262 public static unsafe SimpleStruct* PtrFunc2 (SimpleStruct* a, A.B.C.MethodInfoTestStruct *b)
264 return (SimpleStruct*) 0;
268 public void ToStringWithPointerSignaturesToNonPrimitiveType ()
270 Assert.AreEqual ("SimpleStruct* PtrFunc2(SimpleStruct*, A.B.C.MethodInfoTestStruct*)",
271 this.GetType ().GetMethod ("PtrFunc2").ToString ());
274 public void ToStringGenericMethod ()
276 Assert.AreEqual ("System.Collections.ObjectModel.ReadOnlyCollection`1[T] AsReadOnly[T](T[])",
277 typeof (Array).GetMethod ("AsReadOnly").ToString ());
280 class GBD_A { public virtual void f () {} }
281 class GBD_B : GBD_A { public override void f () {} }
282 class GBD_C : GBD_B { public override void f () {} }
283 class GBD_D : GBD_C { public new virtual void f () {} }
284 class GBD_E : GBD_D { public override void f () {} }
287 public void GetBaseDefinition ()
289 Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
290 Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
291 Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
294 class TestInheritedMethodA {
295 private void TestMethod ()
299 public void TestMethod2 ()
304 class TestInheritedMethodB : TestInheritedMethodA {
308 public void InheritanceTestGetMethodTest ()
310 MethodInfo inheritedMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
311 MethodInfo baseMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
312 Assert.AreSame (inheritedMethod, baseMethod);
314 MethodInfo inheritedMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
315 MethodInfo baseMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
316 Assert.AreSame (inheritedMethod, baseMethod);
321 public void GetMethodBody_Abstract ()
323 MethodBody mb = typeof (ICloneable).GetMethod ("Clone").GetMethodBody ();
328 public void GetMethodBody_Runtime ()
330 MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
335 public void GetMethodBody_Pinvoke ()
337 MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
342 public void GetMethodBody_Icall ()
344 foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
345 if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
346 MethodBody mb = mi.GetMethodBody ();
351 public static void locals_method ()
353 byte[] b = new byte [10];
356 /* This generates a pinned local */
357 fixed (byte *p = &b [0]) {
363 public void GetMethodBody ()
365 #if MONOTOUCH && !DEBUG
366 Assert.Ignore ("Release app (on devices) are stripped of (managed) IL so this test would fail");
368 MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
370 Assert.IsTrue (mb.InitLocals, "#1");
371 Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
373 IList<LocalVariableInfo> locals = mb.LocalVariables;
375 // This might break with different compilers etc.
376 Assert.AreEqual (2, locals.Count, "#3");
378 Assert.IsTrue ((locals [0].LocalType == typeof (byte[])) || (locals [1].LocalType == typeof (byte[])), "#4");
379 if (locals [0].LocalType == typeof (byte[]))
380 Assert.AreEqual (false, locals [0].IsPinned, "#5");
382 Assert.AreEqual (false, locals [1].IsPinned, "#6");
385 public int return_parameter_test ()
391 public void GetMethodFromHandle_Generic ()
393 MethodHandleTest<int> test = new MethodHandleTest<int> ();
394 RuntimeMethodHandle mh = test.GetType ().GetProperty ("MyList")
395 .GetGetMethod ().MethodHandle;
396 MethodBase mb = MethodInfo.GetMethodFromHandle (mh,
397 typeof (MethodHandleTest<int>).TypeHandle);
398 Assert.IsNotNull (mb, "#1");
399 List<int> list = (List<int>) mb.Invoke (test, null);
400 Assert.IsNotNull (list, "#2");
401 Assert.AreEqual (1, list.Count, "#3");
405 public void ReturnParameter ()
407 ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
408 Assert.AreEqual (typeof (int), pi.ParameterType, "#1");
409 Assert.AreEqual (-1, pi.Position, "#2");
410 // MS always return false here
411 //Assert.IsTrue (pi.IsRetval, "#3");
415 public void InvokeOnRefOnlyAssembly ()
417 Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
418 Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
419 MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
421 m.Invoke (null, new object [0]);
423 } catch (InvalidOperationException ex) {
424 // The requested operation is invalid in the
425 // ReflectionOnly context
426 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
427 Assert.IsNull (ex.InnerException, "#3");
428 Assert.IsNotNull (ex.Message, "#4");
433 [ExpectedException (typeof (TargetInvocationException))]
434 public void InvokeInvalidOpExceptionThrow () {
435 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ThrowMethod");
436 mi.Invoke(null, null);
439 public static void ThrowMethod () {
440 throw new InvalidOperationException ();
444 public void InvokeGenericVtype ()
446 KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
447 Type type = kvp.GetType ();
448 Type [] arguments = type.GetGenericArguments ();
449 MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
450 MethodInfo generic_method = method.MakeGenericMethod (arguments);
451 kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
453 Assert.AreEqual ("a", kvp.Key, "#1");
454 Assert.AreEqual (21, kvp.Value, "#2");
457 public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
463 public void InvokeGenericInst ()
465 List<string> str = null;
467 object [] methodArgs = new object [] { str };
468 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("GenericRefMethod");
469 mi.Invoke (null, methodArgs);
470 Assert.IsNotNull (methodArgs [0], "#A1");
471 Assert.IsNull (str, "#A2");
472 Assert.IsTrue (methodArgs [0] is List<string>, "#A3");
474 List<string> refStr = methodArgs [0] as List<string>;
475 Assert.IsNotNull (refStr, "#B1");
476 Assert.AreEqual (1, refStr.Count, "#B2");
477 Assert.AreEqual ("test", refStr [0], "#B3");
480 public static void GenericRefMethod (ref List<string> strArg)
482 strArg = new List<string> ();
486 public void MakeGenericMethodArgsMismatchFoo<T> ()
491 public void MakeGenericMethodArgsMismatch ()
493 MethodInfo gmi = this.GetType ().GetMethod (
494 "MakeGenericMethodArgsMismatchFoo");
496 gmi.MakeGenericMethod ();
498 } catch (ArgumentException ex) {
499 // The type or method has 1 generic parameter(s),
500 // but 0 generic argument(s) were provided. A
501 // generic argument must be provided for each
503 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
504 Assert.IsNull (ex.InnerException, "#3");
505 Assert.IsNotNull (ex.Message, "#4");
506 Assert.IsNull (ex.ParamName, "#5");
510 public void SimpleGenericMethod<TFoo, TBar> () {}
513 public void MakeGenericMethodWithNullArray ()
515 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
517 gmi.MakeGenericMethod ((Type []) null);
519 } catch (ArgumentNullException ex) {
520 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
521 Assert.IsNull (ex.InnerException, "#3");
522 Assert.IsNotNull (ex.Message, "#4");
523 Assert.AreEqual ("methodInstantiation", ex.ParamName, "#5");
528 public void MakeGenericMethodWithNullValueInTypesArray ()
530 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
532 gmi.MakeGenericMethod (new Type [] { typeof (int), null });
534 } catch (ArgumentNullException ex) {
535 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
536 Assert.IsNull (ex.InnerException, "#3");
537 Assert.IsNotNull (ex.Message, "#4");
538 Assert.IsNull (ex.ParamName, "#5");
543 public void MakeGenericMethodWithNonGenericMethodDefinitionMethod ()
545 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
546 MethodInfo inst = gmi.MakeGenericMethod (typeof (int), typeof (double));
548 inst.MakeGenericMethod (typeof (int), typeof (double));
550 } catch (InvalidOperationException ex) {
554 public TFoo SimpleGenericMethod2<TFoo, TBar> () { return default (TFoo); }
555 /*Test for the uggly broken behavior of SRE.*/
557 public void MakeGenericMethodWithSreTypeResultsInStupidMethodInfo ()
559 AssemblyName assemblyName = new AssemblyName ();
560 assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodInfoTest";
561 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
562 ModuleBuilder module = assembly.DefineDynamicModule ("module1", "tst.dll");
563 TypeBuilder tb = module.DefineType ("Test", TypeAttributes.Public);
565 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod2");
566 MethodInfo ins = gmi.MakeGenericMethod (typeof (int), tb);
568 Assert.AreSame (tb, ins.GetGenericArguments () [1], "#1");
569 /*broken ReturnType*/
570 Assert.AreSame (gmi.GetGenericArguments () [0], ins.ReturnType, "#2");
573 public static int? pass_nullable (int? i)
579 public void NullableTests ()
581 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
582 Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
583 Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
585 // Test conversion of vtype to a nullable type for the this argument
586 PropertyInfo pi = typeof (Nullable <int>).GetProperty ("HasValue");
587 Assert.AreEqual (true, pi.GetGetMethod ().Invoke (10, null));
588 PropertyInfo pi2 = typeof (Nullable <int>).GetProperty ("Value");
589 Assert.AreEqual (10, pi2.GetGetMethod ().Invoke (10, null));
592 public static void foo_generic<T> ()
597 public void IsGenericMethod ()
599 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
600 Assert.AreEqual (true, mi.IsGenericMethod, "#1");
601 MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
602 Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
603 MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
604 Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
609 public static void Foo<T2> (T2 i)
613 public static void Bar ()
619 public static void Baz ()
626 public void ContainsGenericParameters ()
628 // Non-generic method in open generic type
629 Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
630 // open generic method in closed generic type
631 Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
632 // non-generic method in closed generic type
633 Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
634 // closed generic method in closed generic type
635 Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
636 // non-generic method in non-generic nested type of closed generic type
637 Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
638 // non-generic method in non-generic nested type of open generic type
639 Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
643 public void IsGenericMethodDefinition ()
645 MethodInfo m1 = typeof (A<>).GetMethod ("Foo");
646 Assert.IsTrue (m1.IsGenericMethod, "#A1");
647 Assert.IsTrue (m1.IsGenericMethodDefinition, "#A2");
649 MethodInfo m2 = typeof (A<int>).GetMethod ("Foo");
650 Assert.IsTrue (m2.IsGenericMethod, "#B1");
651 Assert.IsTrue (m2.IsGenericMethodDefinition, "#B2");
653 MethodInfo m3 = m2.MakeGenericMethod (typeof (int));
654 Assert.IsTrue (m3.IsGenericMethod, "#C1");
655 Assert.IsFalse (m3.IsGenericMethodDefinition, "#C2");
659 public void GetGenericMethodDefinition ()
661 MethodInfo mi1 = typeof (MyList<>).GetMethod ("ConvertAll");
662 MethodInfo mi2 = typeof (MyList<int>).GetMethod ("ConvertAll");
664 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[T,TOutput]",
665 mi1.GetParameters () [0].ParameterType.ToString (), "#A1");
666 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[System.Int32,TOutput]",
667 mi2.GetParameters () [0].ParameterType.ToString (), "#A2");
668 Assert.IsTrue (mi1.IsGenericMethod, "#A3");
669 Assert.IsTrue (mi1.IsGenericMethodDefinition, "#A4");
670 Assert.IsTrue (mi2.IsGenericMethod, "#A5");
671 Assert.IsTrue (mi2.IsGenericMethodDefinition, "#A6");
673 MethodInfo mi3 = mi2.GetGenericMethodDefinition ();
675 Assert.IsTrue (mi3.IsGenericMethod, "#B1");
676 Assert.IsTrue (mi3.IsGenericMethodDefinition, "#B2");
677 Assert.AreSame (mi2, mi3, "#B3");
679 MethodInfo mi4 = mi2.MakeGenericMethod (typeof (short));
680 Assert.IsTrue (mi4.IsGenericMethod, "#C1");
681 Assert.IsFalse (mi4.IsGenericMethodDefinition, "#C2");
682 Assert.AreSame (mi2, mi4.GetGenericMethodDefinition (), "#C3");
685 public void TestMethod123(int a, int b) {}
688 public void GetParametersDontReturnInternedArray ()
690 var method = typeof (MethodInfoTest).GetMethod ("TestMethod123");
691 var parms = method.GetParameters ();
692 Assert.AreNotSame (parms, method.GetParameters (), "#1");
695 Assert.IsNotNull (method.GetParameters () [0], "#2");
699 public void Bug354757 ()
701 MethodInfo gmd = (typeof (MyList <int>)).GetMethod ("ConvertAll");
702 MethodInfo oi = gmd.MakeGenericMethod (gmd.GetGenericArguments ());
703 Assert.AreSame (gmd, oi);
707 [ExpectedException (typeof (ArgumentException))]
709 [Category ("NotWorking")] // #10552
711 public void MakeGenericMethodRespectConstraints ()
713 var m = typeof (MethodInfoTest).GetMethod ("TestMethod");
714 m.MakeGenericMethod (typeof (Type));
717 public void TestMethod <T> () where T : Exception
721 public class MyList<T>
723 public TOutput ConvertAll<TOutput> (Foo<T,TOutput> arg)
725 return default (TOutput);
727 public T ConvertAll2 (MyList<T> arg)
733 public class Foo<T,TOutput>
737 class GenericHelper<T>
739 public void Test (T t)
745 interface IMethodInvoke<out T>
750 class MethodInvoke : IMethodInvoke<string>
752 public string Test ()
754 return "MethodInvoke";
759 public void GetInterfaceMapWorksWithVariantIfaces ()
761 var m0 = typeof (IMethodInvoke<object>).GetMethod ("Test");
762 var m1 = typeof (IMethodInvoke<string>).GetMethod ("Test");
763 var obj = new MethodInvoke ();
765 Assert.AreEqual ("MethodInvoke", m0.Invoke (obj, new Object [0]));
766 Assert.AreEqual ("MethodInvoke", m1.Invoke (obj, new Object [0]));
771 public int? Bug12856 ()
777 public void MethodToStringShouldPrintFullNameOfGenericStructs ()
779 var m = GetType ().GetMethod ("Bug12856");
780 Assert.AreEqual ("System.Nullable`1[System.Int32] Bug12856()", m.ToString (), "#1");
784 class GenericClass<T>
786 public void Method ()
789 Console.WriteLine(lv);
792 public void Method2<K> (T a0, K a1)
796 Console.WriteLine (var0);
797 Console.WriteLine (var1);
802 public void TestLocalVariableTypes ()
804 var typeofT = typeof (GenericClass<>).GetGenericArguments () [0];
805 var typeofK = typeof (GenericClass<>).GetMethod ("Method2").GetGenericArguments () [0];
807 var type = typeof (GenericClass<>).GetMethod("Method").GetMethodBody().LocalVariables[0].LocalType;
808 Assert.AreEqual (typeofT, type);
809 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
811 type = typeof (GenericClass<>).GetMethod("Method2").GetMethodBody().LocalVariables[0].LocalType;
812 Assert.AreEqual (typeofT, type);
813 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
815 type = typeof (GenericClass<>).GetMethod("Method2").GetMethodBody().LocalVariables[1].LocalType;
816 Assert.AreEqual (typeofK, type);
817 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
819 type = typeof (GenericClass<int>).GetMethod("Method2").GetMethodBody().LocalVariables[0].LocalType;
820 Assert.AreEqual (typeof (int), type);
822 type = typeof (GenericClass<int>).GetMethod("Method2").GetMethodBody().LocalVariables[1].LocalType;
823 Assert.AreEqual (typeofK, type);
824 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
831 class RefOnlyMethodClass
833 // Helper static method
834 static void RefOnlyMethod ()
839 public class MethodHandleTest<T>
841 private List<T> _myList = new List<T> ();
843 public MethodHandleTest ()
845 _myList.Add (default (T));
848 public List<T> MyList {
849 get { return _myList; }
850 set { _myList = value; }