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 ()
67 public interface InterfaceTest
73 public void IsDefined_AttributeType_Null ()
75 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
78 mi.IsDefined ((Type) null, false);
80 } catch (ArgumentNullException ex) {
81 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
82 Assert.IsNull (ex.InnerException, "#3");
83 Assert.IsNotNull (ex.Message, "#4");
84 Assert.IsNotNull (ex.ParamName, "#5");
85 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
90 public void TestInvokeByRefReturnMethod ()
93 MethodInfo m = typeof (int[]).GetMethod ("Address");
94 m.Invoke (new int[1], new object[] { 0 });
96 } catch (NotSupportedException e) {
97 Assert.AreEqual (typeof (NotSupportedException), e.GetType (), "#2");
98 Assert.IsNull (e.InnerException, "#3");
99 Assert.IsNotNull (e.Message, "#4");
104 public void PseudoCustomAttributes ()
106 Type t = typeof (MethodInfoTest);
108 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
110 Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
111 Assert.AreEqual ("foo", attr.EntryPoint, "#2");
112 Assert.AreEqual ("libfoo", attr.Value, "#3");
113 Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
114 Assert.AreEqual (false, attr.ExactSpelling, "#5");
115 Assert.AreEqual (true, attr.PreserveSig, "#6");
116 Assert.AreEqual (true, attr.SetLastError, "#7");
117 Assert.AreEqual (true, attr.BestFitMapping, "#8");
118 Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
120 PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
122 // This doesn't work under MS.NET
124 MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
128 [return: MarshalAs (UnmanagedType.Interface)]
129 public void ReturnTypeMarshalAs ()
134 public void ReturnTypePseudoCustomAttributes ()
136 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
138 Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
141 public static int foo (int i, int j)
147 public void StaticInvokeWithObject ()
149 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
151 mi.Invoke (new Object (), new object [] { 1, 2 });
155 public void ByRefInvoke ()
157 MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
158 object[] parms = new object[] {1};
159 met.Invoke (null, parms);
160 Assert.AreEqual (2, parms[0]);
163 public static void ByRefTest (ref int a1)
169 static int byref_arg;
171 public static void ByrefVtype (ref int i) {
177 public void ByrefVtypeInvoke ()
179 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
182 object[] args = new object [] { o };
183 mi.Invoke (null, args);
184 Assert.AreEqual (1, byref_arg, "#A1");
185 Assert.AreEqual (1, o, "#A2");
186 Assert.AreEqual (5, args[0], "#A3");
189 mi.Invoke (null, args);
190 Assert.AreEqual (0, byref_arg, "#B1");
191 Assert.AreEqual (5, args[0], "#B2");
194 public void HeyHey (out string out1, ref DateTime ref1)
199 public void SignatureTest (__arglist)
203 public static unsafe int* PtrFunc (int* a)
209 public void InvokeThreadAbort ()
211 MethodInfo method = typeof (MethodInfoTest).GetMethod ("AbortIt");
213 method.Invoke (null, new object [0]);
216 catch (ThreadAbortException ex) {
217 Thread.ResetAbort ();
218 Assert.IsNull (ex.InnerException, "#2");
222 public static void AbortIt ()
224 Thread.CurrentThread.Abort ();
228 public void ToStringByRef ()
230 Assert.AreEqual ("Void HeyHey(System.String ByRef, System.DateTime ByRef)",
231 this.GetType ().GetMethod ("HeyHey").ToString ());
235 public void ToStringArgList ()
237 Assert.AreEqual ("Void SignatureTest(...)",
238 this.GetType ().GetMethod ("SignatureTest").ToString ());
242 public void ToStringWithPointerSignatures () //bug #409583
244 Assert.AreEqual ("Int32* PtrFunc(Int32*)", this.GetType ().GetMethod ("PtrFunc").ToString ());
247 public struct SimpleStruct
252 public static unsafe SimpleStruct* PtrFunc2 (SimpleStruct* a, A.B.C.MethodInfoTestStruct *b)
254 return (SimpleStruct*) 0;
258 public void ToStringWithPointerSignaturesToNonPrimitiveType ()
260 Assert.AreEqual ("SimpleStruct* PtrFunc2(SimpleStruct*, A.B.C.MethodInfoTestStruct*)",
261 this.GetType ().GetMethod ("PtrFunc2").ToString ());
264 public void ToStringGenericMethod ()
266 Assert.AreEqual ("System.Collections.ObjectModel.ReadOnlyCollection`1[T] AsReadOnly[T](T[])",
267 typeof (Array).GetMethod ("AsReadOnly").ToString ());
270 class GBD_A { public virtual void f () {} }
271 class GBD_B : GBD_A { public override void f () {} }
272 class GBD_C : GBD_B { public override void f () {} }
273 class GBD_D : GBD_C { public new virtual void f () {} }
274 class GBD_E : GBD_D { public override void f () {} }
277 public void GetBaseDefinition ()
279 Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
280 Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
281 Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
284 class TestInheritedMethodA {
285 private void TestMethod ()
289 public void TestMethod2 ()
294 class TestInheritedMethodB : TestInheritedMethodA {
298 public void InheritanceTestGetMethodTest ()
300 MethodInfo inheritedMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
301 MethodInfo baseMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
302 Assert.AreSame (inheritedMethod, baseMethod);
304 MethodInfo inheritedMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
305 MethodInfo baseMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
306 Assert.AreSame (inheritedMethod, baseMethod);
310 public void GetMethodBody_Abstract ()
312 MethodBody mb = typeof (InterfaceTest).GetMethod ("Clone").GetMethodBody ();
317 public void GetMethodBody_Runtime ()
319 MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
324 public void GetMethodBody_Pinvoke ()
326 MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
331 public void GetMethodBody_Icall ()
333 foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
334 if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
335 MethodBody mb = mi.GetMethodBody ();
340 public static void locals_method ()
342 byte[] b = new byte [10];
345 /* This generates a pinned local */
346 fixed (byte *p = &b [0]) {
352 public void GetMethodBody ()
354 #if MONOTOUCH && !DEBUG
355 Assert.Ignore ("Release app (on devices) are stripped of (managed) IL so this test would fail");
357 MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
359 Assert.IsTrue (mb.InitLocals, "#1");
360 Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
362 IList<LocalVariableInfo> locals = mb.LocalVariables;
364 bool foundPinnedBytePointer = false;
366 foreach (LocalVariableInfo lvi in locals) {
367 if (lvi.LocalType == typeof (byte[]))
368 // This is optimized out by CSC in .NET 4.6
369 Assert.IsFalse (lvi.IsPinned, "#3-1");
371 if (/* mcs */ lvi.LocalType == typeof (byte*) || /* csc */ lvi.LocalType == typeof (byte).MakeByRefType ()) {
372 foundPinnedBytePointer = true;
373 Assert.IsTrue (lvi.IsPinned, "#3-2");
377 Assert.IsTrue (foundPinnedBytePointer, "#4");
380 public int return_parameter_test ()
386 public void GetMethodFromHandle_Generic ()
388 MethodHandleTest<int> test = new MethodHandleTest<int> ();
389 RuntimeMethodHandle mh = test.GetType ().GetProperty ("MyList")
390 .GetGetMethod ().MethodHandle;
391 MethodBase mb = MethodInfo.GetMethodFromHandle (mh,
392 typeof (MethodHandleTest<int>).TypeHandle);
393 Assert.IsNotNull (mb, "#1");
394 List<int> list = (List<int>) mb.Invoke (test, null);
395 Assert.IsNotNull (list, "#2");
396 Assert.AreEqual (1, list.Count, "#3");
400 public void ReturnParameter ()
402 ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
403 Assert.AreEqual (typeof (int), pi.ParameterType, "#1");
404 Assert.AreEqual (-1, pi.Position, "#2");
405 // MS always return false here
406 //Assert.IsTrue (pi.IsRetval, "#3");
410 public void MethodInfoModule ()
412 Type type = typeof (MethodInfoTest);
413 MethodInfo me = type.GetMethod ("return_parameter_test");
415 Assert.AreEqual (type.Module, me.Module);
419 public void InvokeOnRefOnlyAssembly ()
421 Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
422 Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
423 MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
425 m.Invoke (null, new object [0]);
427 } catch (InvalidOperationException ex) {
428 // The requested operation is invalid in the
429 // ReflectionOnly context
430 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
431 Assert.IsNull (ex.InnerException, "#3");
432 Assert.IsNotNull (ex.Message, "#4");
437 [ExpectedException (typeof (TargetInvocationException))]
438 public void InvokeInvalidOpExceptionThrow () {
439 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ThrowMethod");
440 mi.Invoke(null, null);
443 public static void ThrowMethod () {
444 throw new InvalidOperationException ();
448 public void InvokeGenericVtype ()
450 KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
451 Type type = kvp.GetType ();
452 Type [] arguments = type.GetGenericArguments ();
453 MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
454 MethodInfo generic_method = method.MakeGenericMethod (arguments);
455 kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
457 Assert.AreEqual ("a", kvp.Key, "#1");
458 Assert.AreEqual (21, kvp.Value, "#2");
461 public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
467 public void InvokeGenericInst ()
469 List<string> str = null;
471 object [] methodArgs = new object [] { str };
472 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("GenericRefMethod");
473 mi.Invoke (null, methodArgs);
474 Assert.IsNotNull (methodArgs [0], "#A1");
475 Assert.IsNull (str, "#A2");
476 Assert.IsTrue (methodArgs [0] is List<string>, "#A3");
478 List<string> refStr = methodArgs [0] as List<string>;
479 Assert.IsNotNull (refStr, "#B1");
480 Assert.AreEqual (1, refStr.Count, "#B2");
481 Assert.AreEqual ("test", refStr [0], "#B3");
484 public static void GenericRefMethod (ref List<string> strArg)
486 strArg = new List<string> ();
490 public void MakeGenericMethodArgsMismatchFoo<T> ()
495 public void MakeGenericMethodArgsMismatch ()
497 MethodInfo gmi = this.GetType ().GetMethod (
498 "MakeGenericMethodArgsMismatchFoo");
500 gmi.MakeGenericMethod ();
502 } catch (ArgumentException ex) {
503 // The type or method has 1 generic parameter(s),
504 // but 0 generic argument(s) were provided. A
505 // generic argument must be provided for each
507 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
508 Assert.IsNull (ex.InnerException, "#3");
509 Assert.IsNotNull (ex.Message, "#4");
510 Assert.IsNull (ex.ParamName, "#5");
514 public void SimpleGenericMethod<TFoo, TBar> () {}
517 public void MakeGenericMethodWithNullArray ()
519 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
521 gmi.MakeGenericMethod ((Type []) null);
523 } catch (ArgumentNullException ex) {
524 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
525 Assert.IsNull (ex.InnerException, "#3");
526 Assert.IsNotNull (ex.Message, "#4");
527 Assert.AreEqual ("methodInstantiation", ex.ParamName, "#5");
532 public void MakeGenericMethodWithNullValueInTypesArray ()
534 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
536 gmi.MakeGenericMethod (new Type [] { typeof (int), null });
538 } catch (ArgumentNullException ex) {
539 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
540 Assert.IsNull (ex.InnerException, "#3");
541 Assert.IsNotNull (ex.Message, "#4");
542 Assert.IsNull (ex.ParamName, "#5");
547 public void MakeGenericMethodWithNonGenericMethodDefinitionMethod ()
549 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
550 MethodInfo inst = gmi.MakeGenericMethod (typeof (int), typeof (double));
552 inst.MakeGenericMethod (typeof (int), typeof (double));
554 } catch (InvalidOperationException ex) {
558 public TFoo SimpleGenericMethod2<TFoo, TBar> () { return default (TFoo); }
559 /*Test for the uggly broken behavior of SRE.*/
561 public void MakeGenericMethodWithSreTypeResultsInStupidMethodInfo ()
563 AssemblyName assemblyName = new AssemblyName ();
564 assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodInfoTest";
565 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
566 ModuleBuilder module = assembly.DefineDynamicModule ("module1", "tst.dll");
567 TypeBuilder tb = module.DefineType ("Test", TypeAttributes.Public);
569 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod2");
570 MethodInfo ins = gmi.MakeGenericMethod (typeof (int), tb);
572 Assert.AreSame (tb, ins.GetGenericArguments () [1], "#1");
573 /*broken ReturnType*/
574 Assert.AreSame (gmi.GetGenericArguments () [0], ins.ReturnType, "#2");
577 public static int? pass_nullable (int? i)
583 public void NullableTests ()
585 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
586 Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
587 Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
589 // Test conversion of vtype to a nullable type for the this argument
590 PropertyInfo pi = typeof (Nullable <int>).GetProperty ("HasValue");
591 Assert.AreEqual (true, pi.GetGetMethod ().Invoke (10, null));
592 PropertyInfo pi2 = typeof (Nullable <int>).GetProperty ("Value");
593 Assert.AreEqual (10, pi2.GetGetMethod ().Invoke (10, null));
596 public static void foo_generic<T> ()
601 public void IsGenericMethod ()
603 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
604 Assert.AreEqual (true, mi.IsGenericMethod, "#1");
605 MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
606 Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
607 MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
608 Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
613 public static void Foo<T2> (T2 i)
617 public static void Bar ()
623 public static void Baz ()
630 public void ContainsGenericParameters ()
632 // Non-generic method in open generic type
633 Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
634 // open generic method in closed generic type
635 Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
636 // non-generic method in closed generic type
637 Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
638 // closed generic method in closed generic type
639 Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
640 // non-generic method in non-generic nested type of closed generic type
641 Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
642 // non-generic method in non-generic nested type of open generic type
643 Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
647 public void IsGenericMethodDefinition ()
649 MethodInfo m1 = typeof (A<>).GetMethod ("Foo");
650 Assert.IsTrue (m1.IsGenericMethod, "#A1");
651 Assert.IsTrue (m1.IsGenericMethodDefinition, "#A2");
653 MethodInfo m2 = typeof (A<int>).GetMethod ("Foo");
654 Assert.IsTrue (m2.IsGenericMethod, "#B1");
655 Assert.IsTrue (m2.IsGenericMethodDefinition, "#B2");
657 MethodInfo m3 = m2.MakeGenericMethod (typeof (int));
658 Assert.IsTrue (m3.IsGenericMethod, "#C1");
659 Assert.IsFalse (m3.IsGenericMethodDefinition, "#C2");
663 public void GetGenericMethodDefinition ()
665 MethodInfo mi1 = typeof (MyList<>).GetMethod ("ConvertAll");
666 MethodInfo mi2 = typeof (MyList<int>).GetMethod ("ConvertAll");
668 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[T,TOutput]",
669 mi1.GetParameters () [0].ParameterType.ToString (), "#A1");
670 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[System.Int32,TOutput]",
671 mi2.GetParameters () [0].ParameterType.ToString (), "#A2");
672 Assert.IsTrue (mi1.IsGenericMethod, "#A3");
673 Assert.IsTrue (mi1.IsGenericMethodDefinition, "#A4");
674 Assert.IsTrue (mi2.IsGenericMethod, "#A5");
675 Assert.IsTrue (mi2.IsGenericMethodDefinition, "#A6");
677 MethodInfo mi3 = mi2.GetGenericMethodDefinition ();
679 Assert.IsTrue (mi3.IsGenericMethod, "#B1");
680 Assert.IsTrue (mi3.IsGenericMethodDefinition, "#B2");
681 Assert.AreSame (mi2, mi3, "#B3");
683 MethodInfo mi4 = mi2.MakeGenericMethod (typeof (short));
684 Assert.IsTrue (mi4.IsGenericMethod, "#C1");
685 Assert.IsFalse (mi4.IsGenericMethodDefinition, "#C2");
686 Assert.AreSame (mi2, mi4.GetGenericMethodDefinition (), "#C3");
689 public void TestMethod123(int a, int b) {}
692 public void GetParametersDontReturnInternedArray ()
694 var method = typeof (MethodInfoTest).GetMethod ("TestMethod123");
695 var parms = method.GetParameters ();
696 Assert.AreNotSame (parms, method.GetParameters (), "#1");
699 Assert.IsNotNull (method.GetParameters () [0], "#2");
703 public void Bug354757 ()
705 MethodInfo gmd = (typeof (MyList <int>)).GetMethod ("ConvertAll");
706 MethodInfo oi = gmd.MakeGenericMethod (gmd.GetGenericArguments ());
707 Assert.AreSame (gmd, oi);
711 [ExpectedException (typeof (ArgumentException))]
713 [Category ("NotWorking")] // #10552
715 public void MakeGenericMethodRespectConstraints ()
717 var m = typeof (MethodInfoTest).GetMethod ("TestMethod");
718 m.MakeGenericMethod (typeof (Type));
721 public void TestMethod <T> () where T : Exception
725 public class MyList<T>
727 public TOutput ConvertAll<TOutput> (Foo<T,TOutput> arg)
729 return default (TOutput);
731 public T ConvertAll2 (MyList<T> arg)
737 public class Foo<T,TOutput>
741 class GenericHelper<T>
743 public void Test (T t)
747 interface IMethodInvoke<out T>
752 class MethodInvoke : IMethodInvoke<string>
754 public string Test ()
756 return "MethodInvoke";
761 public void GetInterfaceMapWorksWithVariantIfaces ()
763 var m0 = typeof (IMethodInvoke<object>).GetMethod ("Test");
764 var m1 = typeof (IMethodInvoke<string>).GetMethod ("Test");
765 var obj = new MethodInvoke ();
767 Assert.AreEqual ("MethodInvoke", m0.Invoke (obj, new Object [0]));
768 Assert.AreEqual ("MethodInvoke", m1.Invoke (obj, new Object [0]));
772 public int? Bug12856 ()
778 public void MethodToStringShouldPrintFullNameOfGenericStructs ()
780 var m = GetType ().GetMethod ("Bug12856");
781 Assert.AreEqual ("System.Nullable`1[System.Int32] Bug12856()", m.ToString (), "#1");
785 class GenericClass<T>
787 public void Method ()
790 Console.WriteLine(lv);
793 public void Method2<K> (T a0, K a1)
797 Console.WriteLine (var0);
798 Console.WriteLine (var1);
803 public void TestLocalVariableTypes ()
805 var typeofT = typeof (GenericClass<>).GetGenericArguments () [0];
806 var typeofK = typeof (GenericClass<>).GetMethod ("Method2").GetGenericArguments () [0];
808 var type = typeof (GenericClass<>).GetMethod("Method").GetMethodBody().LocalVariables[0].LocalType;
809 Assert.AreEqual (typeofT, type);
810 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
812 bool foundTypeOfK = false;
813 bool foundExpectedType = false;
815 MethodBody mb = typeof (GenericClass<>).GetMethod("Method2").GetMethodBody();
816 foreach (LocalVariableInfo lvi in mb.LocalVariables) {
817 if (lvi.LocalType == typeofK) {
819 Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#1-1");
820 } else if (lvi.LocalType == typeofT) {
821 foundExpectedType = true;
822 Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#1-2");
826 Assert.IsTrue (foundTypeOfK, "#1-3");
827 if (mb.LocalVariables.Count < 2)
828 Assert.Ignore ("Code built in release mode - 'T var0' optmized out");
830 Assert.IsTrue (foundExpectedType, "#1-4");
832 foundTypeOfK = false;
833 foundExpectedType = false;
834 mb = typeof (GenericClass<int>).GetMethod("Method2").GetMethodBody();
835 foreach (LocalVariableInfo lvi in mb.LocalVariables) {
836 if (lvi.LocalType == typeofK) {
838 Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#2-1");
839 } else if (lvi.LocalType == typeof (int)) {
840 foundExpectedType = true;
844 Assert.IsTrue (foundTypeOfK, "#2-3");
845 if (mb.LocalVariables.Count < 2)
846 Assert.Ignore ("Code built in release mode - 'int var0' optmized out");
848 Assert.IsTrue (foundExpectedType, "#2-4");
854 class RefOnlyMethodClass
856 // Helper static method
857 static void RefOnlyMethod ()
862 public class MethodHandleTest<T>
864 private List<T> _myList = new List<T> ();
866 public MethodHandleTest ()
868 _myList.Add (default (T));
871 public List<T> MyList {
872 get { return _myList; }
873 set { _myList = value; }