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) {
172 public void ByrefVtypeInvoke ()
174 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
177 object[] args = new object [] { o };
178 mi.Invoke (null, args);
179 Assert.AreEqual (1, byref_arg, "#A1");
180 Assert.AreEqual (1, o, "#A2");
181 Assert.AreEqual (5, args[0], "#A3");
184 mi.Invoke (null, args);
185 Assert.AreEqual (0, byref_arg, "#B1");
186 Assert.AreEqual (5, args[0], "#B2");
189 public void HeyHey (out string out1, ref DateTime ref1)
194 public void SignatureTest (__arglist)
198 public static unsafe int* PtrFunc (int* a)
204 public void InvokeThreadAbort ()
206 MethodInfo method = typeof (MethodInfoTest).GetMethod ("AbortIt");
208 method.Invoke (null, new object [0]);
211 catch (ThreadAbortException ex) {
212 Thread.ResetAbort ();
213 Assert.IsNull (ex.InnerException, "#2");
217 public static void AbortIt ()
219 Thread.CurrentThread.Abort ();
223 public void ToStringByRef ()
225 Assert.AreEqual ("Void HeyHey(System.String ByRef, System.DateTime ByRef)",
226 this.GetType ().GetMethod ("HeyHey").ToString ());
230 public void ToStringArgList ()
232 Assert.AreEqual ("Void SignatureTest(...)",
233 this.GetType ().GetMethod ("SignatureTest").ToString ());
237 public void ToStringWithPointerSignatures () //bug #409583
239 Assert.AreEqual ("Int32* PtrFunc(Int32*)", this.GetType ().GetMethod ("PtrFunc").ToString ());
242 public struct SimpleStruct
247 public static unsafe SimpleStruct* PtrFunc2 (SimpleStruct* a, A.B.C.MethodInfoTestStruct *b)
249 return (SimpleStruct*) 0;
253 public void ToStringWithPointerSignaturesToNonPrimitiveType ()
255 Assert.AreEqual ("SimpleStruct* PtrFunc2(SimpleStruct*, A.B.C.MethodInfoTestStruct*)",
256 this.GetType ().GetMethod ("PtrFunc2").ToString ());
259 public void ToStringGenericMethod ()
261 Assert.AreEqual ("System.Collections.ObjectModel.ReadOnlyCollection`1[T] AsReadOnly[T](T[])",
262 typeof (Array).GetMethod ("AsReadOnly").ToString ());
265 class GBD_A { public virtual void f () {} }
266 class GBD_B : GBD_A { public override void f () {} }
267 class GBD_C : GBD_B { public override void f () {} }
268 class GBD_D : GBD_C { public new virtual void f () {} }
269 class GBD_E : GBD_D { public override void f () {} }
272 public void GetBaseDefinition ()
274 Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
275 Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
276 Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
279 class TestInheritedMethodA {
280 private void TestMethod ()
284 public void TestMethod2 ()
289 class TestInheritedMethodB : TestInheritedMethodA {
293 public void InheritanceTestGetMethodTest ()
295 MethodInfo inheritedMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
296 MethodInfo baseMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
297 Assert.AreSame (inheritedMethod, baseMethod);
299 MethodInfo inheritedMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
300 MethodInfo baseMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
301 Assert.AreSame (inheritedMethod, baseMethod);
305 public void GetMethodBody_Abstract ()
307 MethodBody mb = typeof (ICloneable).GetMethod ("Clone").GetMethodBody ();
312 public void GetMethodBody_Runtime ()
314 MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
319 public void GetMethodBody_Pinvoke ()
321 MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
326 public void GetMethodBody_Icall ()
328 foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
329 if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
330 MethodBody mb = mi.GetMethodBody ();
335 public static void locals_method ()
337 byte[] b = new byte [10];
340 /* This generates a pinned local */
341 fixed (byte *p = &b [0]) {
347 public void GetMethodBody ()
349 #if MONOTOUCH && !DEBUG
350 Assert.Ignore ("Release app (on devices) are stripped of (managed) IL so this test would fail");
352 MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
354 Assert.IsTrue (mb.InitLocals, "#1");
355 Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
357 IList<LocalVariableInfo> locals = mb.LocalVariables;
359 // This might break with different compilers etc.
360 Assert.AreEqual (2, locals.Count, "#3");
362 Assert.IsTrue ((locals [0].LocalType == typeof (byte[])) || (locals [1].LocalType == typeof (byte[])), "#4");
363 if (locals [0].LocalType == typeof (byte[]))
364 Assert.AreEqual (false, locals [0].IsPinned, "#5");
366 Assert.AreEqual (false, locals [1].IsPinned, "#6");
369 public int return_parameter_test ()
375 public void GetMethodFromHandle_Generic ()
377 MethodHandleTest<int> test = new MethodHandleTest<int> ();
378 RuntimeMethodHandle mh = test.GetType ().GetProperty ("MyList")
379 .GetGetMethod ().MethodHandle;
380 MethodBase mb = MethodInfo.GetMethodFromHandle (mh,
381 typeof (MethodHandleTest<int>).TypeHandle);
382 Assert.IsNotNull (mb, "#1");
383 List<int> list = (List<int>) mb.Invoke (test, null);
384 Assert.IsNotNull (list, "#2");
385 Assert.AreEqual (1, list.Count, "#3");
389 public void ReturnParameter ()
391 ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
392 Assert.AreEqual (typeof (int), pi.ParameterType, "#1");
393 Assert.AreEqual (-1, pi.Position, "#2");
394 // MS always return false here
395 //Assert.IsTrue (pi.IsRetval, "#3");
399 public void InvokeOnRefOnlyAssembly ()
401 Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
402 Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
403 MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
405 m.Invoke (null, new object [0]);
407 } catch (InvalidOperationException ex) {
408 // The requested operation is invalid in the
409 // ReflectionOnly context
410 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
411 Assert.IsNull (ex.InnerException, "#3");
412 Assert.IsNotNull (ex.Message, "#4");
417 [ExpectedException (typeof (TargetInvocationException))]
418 public void InvokeInvalidOpExceptionThrow () {
419 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ThrowMethod");
420 mi.Invoke(null, null);
423 public static void ThrowMethod () {
424 throw new InvalidOperationException ();
428 public void InvokeGenericVtype ()
430 KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
431 Type type = kvp.GetType ();
432 Type [] arguments = type.GetGenericArguments ();
433 MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
434 MethodInfo generic_method = method.MakeGenericMethod (arguments);
435 kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
437 Assert.AreEqual ("a", kvp.Key, "#1");
438 Assert.AreEqual (21, kvp.Value, "#2");
441 public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
447 public void InvokeGenericInst ()
449 List<string> str = null;
451 object [] methodArgs = new object [] { str };
452 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("GenericRefMethod");
453 mi.Invoke (null, methodArgs);
454 Assert.IsNotNull (methodArgs [0], "#A1");
455 Assert.IsNull (str, "#A2");
456 Assert.IsTrue (methodArgs [0] is List<string>, "#A3");
458 List<string> refStr = methodArgs [0] as List<string>;
459 Assert.IsNotNull (refStr, "#B1");
460 Assert.AreEqual (1, refStr.Count, "#B2");
461 Assert.AreEqual ("test", refStr [0], "#B3");
464 public static void GenericRefMethod (ref List<string> strArg)
466 strArg = new List<string> ();
470 public void MakeGenericMethodArgsMismatchFoo<T> ()
475 public void MakeGenericMethodArgsMismatch ()
477 MethodInfo gmi = this.GetType ().GetMethod (
478 "MakeGenericMethodArgsMismatchFoo");
480 gmi.MakeGenericMethod ();
482 } catch (ArgumentException ex) {
483 // The type or method has 1 generic parameter(s),
484 // but 0 generic argument(s) were provided. A
485 // generic argument must be provided for each
487 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
488 Assert.IsNull (ex.InnerException, "#3");
489 Assert.IsNotNull (ex.Message, "#4");
490 Assert.IsNull (ex.ParamName, "#5");
494 public void SimpleGenericMethod<TFoo, TBar> () {}
497 public void MakeGenericMethodWithNullArray ()
499 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
501 gmi.MakeGenericMethod ((Type []) null);
503 } catch (ArgumentNullException ex) {
504 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
505 Assert.IsNull (ex.InnerException, "#3");
506 Assert.IsNotNull (ex.Message, "#4");
507 Assert.AreEqual ("methodInstantiation", ex.ParamName, "#5");
512 public void MakeGenericMethodWithNullValueInTypesArray ()
514 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
516 gmi.MakeGenericMethod (new Type [] { typeof (int), null });
518 } catch (ArgumentNullException ex) {
519 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
520 Assert.IsNull (ex.InnerException, "#3");
521 Assert.IsNotNull (ex.Message, "#4");
522 Assert.IsNull (ex.ParamName, "#5");
527 public void MakeGenericMethodWithNonGenericMethodDefinitionMethod ()
529 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
530 MethodInfo inst = gmi.MakeGenericMethod (typeof (int), typeof (double));
532 inst.MakeGenericMethod (typeof (int), typeof (double));
534 } catch (InvalidOperationException ex) {
538 public TFoo SimpleGenericMethod2<TFoo, TBar> () { return default (TFoo); }
539 /*Test for the uggly broken behavior of SRE.*/
541 public void MakeGenericMethodWithSreTypeResultsInStupidMethodInfo ()
543 AssemblyName assemblyName = new AssemblyName ();
544 assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodInfoTest";
545 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
546 ModuleBuilder module = assembly.DefineDynamicModule ("module1", "tst.dll");
547 TypeBuilder tb = module.DefineType ("Test", TypeAttributes.Public);
549 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod2");
550 MethodInfo ins = gmi.MakeGenericMethod (typeof (int), tb);
552 Assert.AreSame (tb, ins.GetGenericArguments () [1], "#1");
553 /*broken ReturnType*/
554 Assert.AreSame (gmi.GetGenericArguments () [0], ins.ReturnType, "#2");
557 public static int? pass_nullable (int? i)
563 public void NullableTests ()
565 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
566 Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
567 Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
569 // Test conversion of vtype to a nullable type for the this argument
570 PropertyInfo pi = typeof (Nullable <int>).GetProperty ("HasValue");
571 Assert.AreEqual (true, pi.GetGetMethod ().Invoke (10, null));
572 PropertyInfo pi2 = typeof (Nullable <int>).GetProperty ("Value");
573 Assert.AreEqual (10, pi2.GetGetMethod ().Invoke (10, null));
576 public static void foo_generic<T> ()
581 public void IsGenericMethod ()
583 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
584 Assert.AreEqual (true, mi.IsGenericMethod, "#1");
585 MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
586 Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
587 MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
588 Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
593 public static void Foo<T2> (T2 i)
597 public static void Bar ()
603 public static void Baz ()
610 public void ContainsGenericParameters ()
612 // Non-generic method in open generic type
613 Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
614 // open generic method in closed generic type
615 Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
616 // non-generic method in closed generic type
617 Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
618 // closed generic method in closed generic type
619 Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
620 // non-generic method in non-generic nested type of closed generic type
621 Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
622 // non-generic method in non-generic nested type of open generic type
623 Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
627 public void IsGenericMethodDefinition ()
629 MethodInfo m1 = typeof (A<>).GetMethod ("Foo");
630 Assert.IsTrue (m1.IsGenericMethod, "#A1");
631 Assert.IsTrue (m1.IsGenericMethodDefinition, "#A2");
633 MethodInfo m2 = typeof (A<int>).GetMethod ("Foo");
634 Assert.IsTrue (m2.IsGenericMethod, "#B1");
635 Assert.IsTrue (m2.IsGenericMethodDefinition, "#B2");
637 MethodInfo m3 = m2.MakeGenericMethod (typeof (int));
638 Assert.IsTrue (m3.IsGenericMethod, "#C1");
639 Assert.IsFalse (m3.IsGenericMethodDefinition, "#C2");
643 public void GetGenericMethodDefinition ()
645 MethodInfo mi1 = typeof (MyList<>).GetMethod ("ConvertAll");
646 MethodInfo mi2 = typeof (MyList<int>).GetMethod ("ConvertAll");
648 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[T,TOutput]",
649 mi1.GetParameters () [0].ParameterType.ToString (), "#A1");
650 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[System.Int32,TOutput]",
651 mi2.GetParameters () [0].ParameterType.ToString (), "#A2");
652 Assert.IsTrue (mi1.IsGenericMethod, "#A3");
653 Assert.IsTrue (mi1.IsGenericMethodDefinition, "#A4");
654 Assert.IsTrue (mi2.IsGenericMethod, "#A5");
655 Assert.IsTrue (mi2.IsGenericMethodDefinition, "#A6");
657 MethodInfo mi3 = mi2.GetGenericMethodDefinition ();
659 Assert.IsTrue (mi3.IsGenericMethod, "#B1");
660 Assert.IsTrue (mi3.IsGenericMethodDefinition, "#B2");
661 Assert.AreSame (mi2, mi3, "#B3");
663 MethodInfo mi4 = mi2.MakeGenericMethod (typeof (short));
664 Assert.IsTrue (mi4.IsGenericMethod, "#C1");
665 Assert.IsFalse (mi4.IsGenericMethodDefinition, "#C2");
666 Assert.AreSame (mi2, mi4.GetGenericMethodDefinition (), "#C3");
669 public void TestMethod123(int a, int b) {}
672 public void GetParametersDontReturnInternedArray ()
674 var method = typeof (MethodInfoTest).GetMethod ("TestMethod123");
675 var parms = method.GetParameters ();
676 Assert.AreNotSame (parms, method.GetParameters (), "#1");
679 Assert.IsNotNull (method.GetParameters () [0], "#2");
683 public void Bug354757 ()
685 MethodInfo gmd = (typeof (MyList <int>)).GetMethod ("ConvertAll");
686 MethodInfo oi = gmd.MakeGenericMethod (gmd.GetGenericArguments ());
687 Assert.AreSame (gmd, oi);
691 [ExpectedException (typeof (ArgumentException))]
693 [Category ("NotWorking")] // #10552
695 public void MakeGenericMethodRespectConstraints ()
697 var m = typeof (MethodInfoTest).GetMethod ("TestMethod");
698 m.MakeGenericMethod (typeof (Type));
701 public void TestMethod <T> () where T : Exception
705 public class MyList<T>
707 public TOutput ConvertAll<TOutput> (Foo<T,TOutput> arg)
709 return default (TOutput);
711 public T ConvertAll2 (MyList<T> arg)
717 public class Foo<T,TOutput>
721 class GenericHelper<T>
723 public void Test (T t)
728 interface IMethodInvoke<out T>
733 class MethodInvoke : IMethodInvoke<string>
735 public string Test ()
737 return "MethodInvoke";
742 public void GetInterfaceMapWorksWithVariantIfaces ()
744 var m0 = typeof (IMethodInvoke<object>).GetMethod ("Test");
745 var m1 = typeof (IMethodInvoke<string>).GetMethod ("Test");
746 var obj = new MethodInvoke ();
748 Assert.AreEqual ("MethodInvoke", m0.Invoke (obj, new Object [0]));
749 Assert.AreEqual ("MethodInvoke", m1.Invoke (obj, new Object [0]));
754 public int? Bug12856 ()
760 public void MethodToStringShouldPrintFullNameOfGenericStructs ()
762 var m = GetType ().GetMethod ("Bug12856");
763 Assert.AreEqual ("System.Nullable`1[System.Int32] Bug12856()", m.ToString (), "#1");
767 class GenericClass<T>
769 public void Method ()
772 Console.WriteLine(lv);
775 public void Method2<K> (T a0, K a1)
779 Console.WriteLine (var0);
780 Console.WriteLine (var1);
785 public void TestLocalVariableTypes ()
787 var typeofT = typeof (GenericClass<>).GetGenericArguments () [0];
788 var typeofK = typeof (GenericClass<>).GetMethod ("Method2").GetGenericArguments () [0];
790 var type = typeof (GenericClass<>).GetMethod("Method").GetMethodBody().LocalVariables[0].LocalType;
791 Assert.AreEqual (typeofT, type);
792 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
794 type = typeof (GenericClass<>).GetMethod("Method2").GetMethodBody().LocalVariables[0].LocalType;
795 Assert.AreEqual (typeofT, type);
796 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
798 type = typeof (GenericClass<>).GetMethod("Method2").GetMethodBody().LocalVariables[1].LocalType;
799 Assert.AreEqual (typeofK, type);
800 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
802 type = typeof (GenericClass<int>).GetMethod("Method2").GetMethodBody().LocalVariables[0].LocalType;
803 Assert.AreEqual (typeof (int), type);
805 type = typeof (GenericClass<int>).GetMethod("Method2").GetMethodBody().LocalVariables[1].LocalType;
806 Assert.AreEqual (typeofK, type);
807 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
813 class RefOnlyMethodClass
815 // Helper static method
816 static void RefOnlyMethod ()
821 public class MethodHandleTest<T>
823 private List<T> _myList = new List<T> ();
825 public MethodHandleTest ()
827 _myList.Add (default (T));
830 public List<T> MyList {
831 get { return _myList; }
832 set { _myList = value; }