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
56 // use an existing symbol - so we can build without dlsym. It does not matter that the signature does not match for the test
57 [DllImport ("libc", EntryPoint="readlink", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
59 [DllImport ("libfoo", EntryPoint="foo", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
61 public static extern void dllImportMethod ();
62 [MethodImplAttribute(MethodImplOptions.PreserveSig)]
63 public void preserveSigMethod ()
67 [MethodImplAttribute(MethodImplOptions.Synchronized)]
68 public void synchronizedMethod ()
72 public interface InterfaceTest
78 public void IsDefined_AttributeType_Null ()
80 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
83 mi.IsDefined ((Type) null, false);
85 } catch (ArgumentNullException ex) {
86 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
87 Assert.IsNull (ex.InnerException, "#3");
88 Assert.IsNotNull (ex.Message, "#4");
89 Assert.IsNotNull (ex.ParamName, "#5");
90 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
95 public void TestInvokeByRefReturnMethod ()
98 MethodInfo m = typeof (int[]).GetMethod ("Address");
99 m.Invoke (new int[1], new object[] { 0 });
101 } catch (NotSupportedException e) {
102 Assert.AreEqual (typeof (NotSupportedException), e.GetType (), "#2");
103 Assert.IsNull (e.InnerException, "#3");
104 Assert.IsNotNull (e.Message, "#4");
109 public void PseudoCustomAttributes ()
111 Type t = typeof (MethodInfoTest);
113 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
115 Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
117 Assert.AreEqual ("readlink", attr.EntryPoint, "#2");
118 Assert.AreEqual ("libc", attr.Value, "#3");
120 Assert.AreEqual ("foo", attr.EntryPoint, "#2");
121 Assert.AreEqual ("libfoo", attr.Value, "#3");
123 Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
124 Assert.AreEqual (false, attr.ExactSpelling, "#5");
125 Assert.AreEqual (true, attr.PreserveSig, "#6");
126 Assert.AreEqual (true, attr.SetLastError, "#7");
127 Assert.AreEqual (true, attr.BestFitMapping, "#8");
128 Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
130 PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
132 // This doesn't work under MS.NET
134 MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
138 [return: MarshalAs (UnmanagedType.Interface)]
139 public void ReturnTypeMarshalAs ()
144 public void ReturnTypePseudoCustomAttributes ()
146 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
148 Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
151 public static int foo (int i, int j)
157 public void StaticInvokeWithObject ()
159 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
161 mi.Invoke (new Object (), new object [] { 1, 2 });
165 public void ByRefInvoke ()
167 MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
168 object[] parms = new object[] {1};
169 met.Invoke (null, parms);
170 Assert.AreEqual (2, parms[0]);
173 public static void ByRefTest (ref int a1)
179 static int byref_arg;
181 public static void ByrefVtype (ref int i) {
187 public void ByrefVtypeInvoke ()
189 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
192 object[] args = new object [] { o };
193 mi.Invoke (null, args);
194 Assert.AreEqual (1, byref_arg, "#A1");
195 Assert.AreEqual (1, o, "#A2");
196 Assert.AreEqual (5, args[0], "#A3");
199 mi.Invoke (null, args);
200 Assert.AreEqual (0, byref_arg, "#B1");
201 Assert.AreEqual (5, args[0], "#B2");
204 public void HeyHey (out string out1, ref DateTime ref1)
209 public void SignatureTest (__arglist)
213 public static unsafe int* PtrFunc (int* a)
218 #if MONO_FEATURE_THREAD_ABORT
220 public void InvokeThreadAbort ()
222 MethodInfo method = typeof (MethodInfoTest).GetMethod ("AbortIt");
224 method.Invoke (null, new object [0]);
227 catch (ThreadAbortException ex) {
228 Thread.ResetAbort ();
229 Assert.IsNull (ex.InnerException, "#2");
233 public static void AbortIt ()
235 Thread.CurrentThread.Abort ();
240 public void ToStringByRef ()
242 Assert.AreEqual ("Void HeyHey(System.String ByRef, System.DateTime ByRef)",
243 this.GetType ().GetMethod ("HeyHey").ToString ());
247 public void ToStringArgList ()
249 Assert.AreEqual ("Void SignatureTest(...)",
250 this.GetType ().GetMethod ("SignatureTest").ToString ());
254 public void ToStringWithPointerSignatures () //bug #409583
256 Assert.AreEqual ("Int32* PtrFunc(Int32*)", this.GetType ().GetMethod ("PtrFunc").ToString ());
259 public struct SimpleStruct
264 public static unsafe SimpleStruct* PtrFunc2 (SimpleStruct* a, A.B.C.MethodInfoTestStruct *b)
266 return (SimpleStruct*) 0;
270 public void ToStringWithPointerSignaturesToNonPrimitiveType ()
272 Assert.AreEqual ("SimpleStruct* PtrFunc2(SimpleStruct*, A.B.C.MethodInfoTestStruct*)",
273 this.GetType ().GetMethod ("PtrFunc2").ToString ());
276 public void ToStringGenericMethod ()
278 Assert.AreEqual ("System.Collections.ObjectModel.ReadOnlyCollection`1[T] AsReadOnly[T](T[])",
279 typeof (Array).GetMethod ("AsReadOnly").ToString ());
282 class GBD_A { public virtual void f () {} }
283 class GBD_B : GBD_A { public override void f () {} }
284 class GBD_C : GBD_B { public override void f () {} }
285 class GBD_D : GBD_C { public new virtual void f () {} }
286 class GBD_E : GBD_D { public override void f () {} }
289 public void GetBaseDefinition ()
291 Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
292 Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
293 Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
296 class TestInheritedMethodA {
297 private void TestMethod ()
301 public void TestMethod2 ()
306 class TestInheritedMethodB : TestInheritedMethodA {
310 public void InheritanceTestGetMethodTest ()
312 MethodInfo inheritedMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
313 MethodInfo baseMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
314 Assert.AreSame (inheritedMethod, baseMethod);
316 MethodInfo inheritedMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
317 MethodInfo baseMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
318 Assert.AreSame (inheritedMethod, baseMethod);
322 public void GetMethodBody_Abstract ()
324 MethodBody mb = typeof (InterfaceTest).GetMethod ("Clone").GetMethodBody ();
329 public void GetMethodBody_Runtime ()
331 MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
336 public void GetMethodBody_Pinvoke ()
338 MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
343 public void GetMethodBody_Icall ()
345 foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
346 if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
347 MethodBody mb = mi.GetMethodBody ();
352 public static void locals_method ()
354 byte[] b = new byte [10];
357 /* This generates a pinned local */
358 fixed (byte *p = &b [0]) {
364 public void GetMethodBody ()
366 #if MONOTOUCH && !DEBUG
367 Assert.Ignore ("Release app (on devices) are stripped of (managed) IL so this test would fail");
369 MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
371 Assert.IsTrue (mb.InitLocals, "#1");
372 Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
374 IList<LocalVariableInfo> locals = mb.LocalVariables;
376 bool foundPinnedBytePointer = false;
378 foreach (LocalVariableInfo lvi in locals) {
379 if (lvi.LocalType == typeof (byte[]))
380 // This is optimized out by CSC in .NET 4.6
381 Assert.IsFalse (lvi.IsPinned, "#3-1");
383 if (/* mcs */ lvi.LocalType == typeof (byte*) || /* csc */ lvi.LocalType == typeof (byte).MakeByRefType ()) {
384 foundPinnedBytePointer = true;
385 Assert.IsTrue (lvi.IsPinned, "#3-2");
389 Assert.IsTrue (foundPinnedBytePointer, "#4");
392 public int return_parameter_test ()
398 public void GetMethodFromHandle_Generic ()
400 MethodHandleTest<int> test = new MethodHandleTest<int> ();
401 RuntimeMethodHandle mh = test.GetType ().GetProperty ("MyList")
402 .GetGetMethod ().MethodHandle;
403 MethodBase mb = MethodInfo.GetMethodFromHandle (mh,
404 typeof (MethodHandleTest<int>).TypeHandle);
405 Assert.IsNotNull (mb, "#1");
406 List<int> list = (List<int>) mb.Invoke (test, null);
407 Assert.IsNotNull (list, "#2");
408 Assert.AreEqual (1, list.Count, "#3");
412 public void ReturnParameter ()
414 ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
415 Assert.AreEqual (typeof (int), pi.ParameterType, "#1");
416 Assert.AreEqual (-1, pi.Position, "#2");
417 // MS always return false here
418 //Assert.IsTrue (pi.IsRetval, "#3");
422 public void MethodInfoModule ()
424 Type type = typeof (MethodInfoTest);
425 MethodInfo me = type.GetMethod ("return_parameter_test");
427 Assert.AreEqual (type.Module, me.Module);
431 public void InvokeOnRefOnlyAssembly ()
433 Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
434 Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
435 MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
437 m.Invoke (null, new object [0]);
439 } catch (InvalidOperationException ex) {
440 // The requested operation is invalid in the
441 // ReflectionOnly context
442 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
443 Assert.IsNull (ex.InnerException, "#3");
444 Assert.IsNotNull (ex.Message, "#4");
449 [ExpectedException (typeof (TargetInvocationException))]
450 public void InvokeInvalidOpExceptionThrow () {
451 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ThrowMethod");
452 mi.Invoke(null, null);
455 public static void ThrowMethod () {
456 throw new InvalidOperationException ();
460 public void InvokeGenericVtype ()
462 KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
463 Type type = kvp.GetType ();
464 Type [] arguments = type.GetGenericArguments ();
465 MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
466 MethodInfo generic_method = method.MakeGenericMethod (arguments);
467 kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
469 Assert.AreEqual ("a", kvp.Key, "#1");
470 Assert.AreEqual (21, kvp.Value, "#2");
473 public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
479 public void InvokeGenericInst ()
481 List<string> str = null;
483 object [] methodArgs = new object [] { str };
484 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("GenericRefMethod");
485 mi.Invoke (null, methodArgs);
486 Assert.IsNotNull (methodArgs [0], "#A1");
487 Assert.IsNull (str, "#A2");
488 Assert.IsTrue (methodArgs [0] is List<string>, "#A3");
490 List<string> refStr = methodArgs [0] as List<string>;
491 Assert.IsNotNull (refStr, "#B1");
492 Assert.AreEqual (1, refStr.Count, "#B2");
493 Assert.AreEqual ("test", refStr [0], "#B3");
496 public static void GenericRefMethod (ref List<string> strArg)
498 strArg = new List<string> ();
502 public void MakeGenericMethodArgsMismatchFoo<T> ()
507 public void MakeGenericMethodArgsMismatch ()
509 MethodInfo gmi = this.GetType ().GetMethod (
510 "MakeGenericMethodArgsMismatchFoo");
512 gmi.MakeGenericMethod ();
514 } catch (ArgumentException ex) {
515 // The type or method has 1 generic parameter(s),
516 // but 0 generic argument(s) were provided. A
517 // generic argument must be provided for each
519 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
520 Assert.IsNull (ex.InnerException, "#3");
521 Assert.IsNotNull (ex.Message, "#4");
522 Assert.IsNull (ex.ParamName, "#5");
526 public void SimpleGenericMethod<TFoo, TBar> () {}
529 public void MakeGenericMethodWithNullArray ()
531 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
533 gmi.MakeGenericMethod ((Type []) null);
535 } catch (ArgumentNullException ex) {
536 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
537 Assert.IsNull (ex.InnerException, "#3");
538 Assert.IsNotNull (ex.Message, "#4");
539 Assert.AreEqual ("methodInstantiation", ex.ParamName, "#5");
544 public void MakeGenericMethodWithNullValueInTypesArray ()
546 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
548 gmi.MakeGenericMethod (new Type [] { typeof (int), null });
550 } catch (ArgumentNullException ex) {
551 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
552 Assert.IsNull (ex.InnerException, "#3");
553 Assert.IsNotNull (ex.Message, "#4");
554 Assert.IsNull (ex.ParamName, "#5");
559 public void MakeGenericMethodWithNonGenericMethodDefinitionMethod ()
561 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
562 MethodInfo inst = gmi.MakeGenericMethod (typeof (int), typeof (double));
564 inst.MakeGenericMethod (typeof (int), typeof (double));
566 } catch (InvalidOperationException ex) {
570 public TFoo SimpleGenericMethod2<TFoo, TBar> () { return default (TFoo); }
571 /*Test for the uggly broken behavior of SRE.*/
573 public void MakeGenericMethodWithSreTypeResultsInStupidMethodInfo ()
575 AssemblyName assemblyName = new AssemblyName ();
576 assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodInfoTest";
577 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
578 ModuleBuilder module = assembly.DefineDynamicModule ("module1", "tst.dll");
579 TypeBuilder tb = module.DefineType ("Test", TypeAttributes.Public);
581 MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod2");
582 MethodInfo ins = gmi.MakeGenericMethod (typeof (int), tb);
584 Assert.AreSame (tb, ins.GetGenericArguments () [1], "#1");
585 /*broken ReturnType*/
586 Assert.AreSame (gmi.GetGenericArguments () [0], ins.ReturnType, "#2");
589 public static int? pass_nullable (int? i)
595 public void NullableTests ()
597 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
598 Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
599 Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
601 // Test conversion of vtype to a nullable type for the this argument
602 PropertyInfo pi = typeof (Nullable <int>).GetProperty ("HasValue");
603 Assert.AreEqual (true, pi.GetGetMethod ().Invoke (10, null));
604 PropertyInfo pi2 = typeof (Nullable <int>).GetProperty ("Value");
605 Assert.AreEqual (10, pi2.GetGetMethod ().Invoke (10, null));
608 public static void foo_generic<T> ()
613 public void IsGenericMethod ()
615 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
616 Assert.AreEqual (true, mi.IsGenericMethod, "#1");
617 MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
618 Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
619 MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
620 Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
625 public static void Foo<T2> (T2 i)
629 public static void Bar ()
635 public static void Baz ()
642 public void ContainsGenericParameters ()
644 // Non-generic method in open generic type
645 Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
646 // open generic method in closed generic type
647 Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
648 // non-generic method in closed generic type
649 Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
650 // closed generic method in closed generic type
651 Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
652 // non-generic method in non-generic nested type of closed generic type
653 Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
654 // non-generic method in non-generic nested type of open generic type
655 Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
659 public void IsGenericMethodDefinition ()
661 MethodInfo m1 = typeof (A<>).GetMethod ("Foo");
662 Assert.IsTrue (m1.IsGenericMethod, "#A1");
663 Assert.IsTrue (m1.IsGenericMethodDefinition, "#A2");
665 MethodInfo m2 = typeof (A<int>).GetMethod ("Foo");
666 Assert.IsTrue (m2.IsGenericMethod, "#B1");
667 Assert.IsTrue (m2.IsGenericMethodDefinition, "#B2");
669 MethodInfo m3 = m2.MakeGenericMethod (typeof (int));
670 Assert.IsTrue (m3.IsGenericMethod, "#C1");
671 Assert.IsFalse (m3.IsGenericMethodDefinition, "#C2");
675 public void GetGenericMethodDefinition ()
677 MethodInfo mi1 = typeof (MyList<>).GetMethod ("ConvertAll");
678 MethodInfo mi2 = typeof (MyList<int>).GetMethod ("ConvertAll");
680 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[T,TOutput]",
681 mi1.GetParameters () [0].ParameterType.ToString (), "#A1");
682 Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[System.Int32,TOutput]",
683 mi2.GetParameters () [0].ParameterType.ToString (), "#A2");
684 Assert.IsTrue (mi1.IsGenericMethod, "#A3");
685 Assert.IsTrue (mi1.IsGenericMethodDefinition, "#A4");
686 Assert.IsTrue (mi2.IsGenericMethod, "#A5");
687 Assert.IsTrue (mi2.IsGenericMethodDefinition, "#A6");
689 MethodInfo mi3 = mi2.GetGenericMethodDefinition ();
691 Assert.IsTrue (mi3.IsGenericMethod, "#B1");
692 Assert.IsTrue (mi3.IsGenericMethodDefinition, "#B2");
693 Assert.AreSame (mi2, mi3, "#B3");
695 MethodInfo mi4 = mi2.MakeGenericMethod (typeof (short));
696 Assert.IsTrue (mi4.IsGenericMethod, "#C1");
697 Assert.IsFalse (mi4.IsGenericMethodDefinition, "#C2");
698 Assert.AreSame (mi2, mi4.GetGenericMethodDefinition (), "#C3");
701 public void TestMethod123(int a, int b) {}
704 public void GetParametersDontReturnInternedArray ()
706 var method = typeof (MethodInfoTest).GetMethod ("TestMethod123");
707 var parms = method.GetParameters ();
708 Assert.AreNotSame (parms, method.GetParameters (), "#1");
711 Assert.IsNotNull (method.GetParameters () [0], "#2");
715 public void Bug354757 ()
717 MethodInfo gmd = (typeof (MyList <int>)).GetMethod ("ConvertAll");
718 MethodInfo oi = gmd.MakeGenericMethod (gmd.GetGenericArguments ());
719 Assert.AreSame (gmd, oi);
723 [ExpectedException (typeof (ArgumentException))]
725 [Category ("NotWorking")] // #10552
727 public void MakeGenericMethodRespectConstraints ()
729 var m = typeof (MethodInfoTest).GetMethod ("TestMethod");
730 m.MakeGenericMethod (typeof (Type));
733 public void TestMethod <T> () where T : Exception
737 public class MyList<T>
739 public TOutput ConvertAll<TOutput> (Foo<T,TOutput> arg)
741 return default (TOutput);
743 public T ConvertAll2 (MyList<T> arg)
749 public class Foo<T,TOutput>
753 class GenericHelper<T>
755 public void Test (T t)
759 interface IMethodInvoke<out T>
764 class MethodInvoke : IMethodInvoke<string>
766 public string Test ()
768 return "MethodInvoke";
773 public void GetInterfaceMapWorksWithVariantIfaces ()
775 var m0 = typeof (IMethodInvoke<object>).GetMethod ("Test");
776 var m1 = typeof (IMethodInvoke<string>).GetMethod ("Test");
777 var obj = new MethodInvoke ();
779 Assert.AreEqual ("MethodInvoke", m0.Invoke (obj, new Object [0]));
780 Assert.AreEqual ("MethodInvoke", m1.Invoke (obj, new Object [0]));
784 public int? Bug12856 ()
790 public void MethodToStringShouldPrintFullNameOfGenericStructs ()
792 var m = GetType ().GetMethod ("Bug12856");
793 Assert.AreEqual ("System.Nullable`1[System.Int32] Bug12856()", m.ToString (), "#1");
797 class GenericClass<T>
799 public void Method ()
802 Console.WriteLine(lv);
805 public void Method2<K> (T a0, K a1)
809 Console.WriteLine (var0);
810 Console.WriteLine (var1);
815 public void TestLocalVariableTypes ()
817 var typeofT = typeof (GenericClass<>).GetGenericArguments () [0];
818 var typeofK = typeof (GenericClass<>).GetMethod ("Method2").GetGenericArguments () [0];
820 var type = typeof (GenericClass<>).GetMethod("Method").GetMethodBody().LocalVariables[0].LocalType;
821 Assert.AreEqual (typeofT, type);
822 Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
824 bool foundTypeOfK = false;
825 bool foundExpectedType = false;
827 MethodBody mb = typeof (GenericClass<>).GetMethod("Method2").GetMethodBody();
828 foreach (LocalVariableInfo lvi in mb.LocalVariables) {
829 if (lvi.LocalType == typeofK) {
831 Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#1-1");
832 } else if (lvi.LocalType == typeofT) {
833 foundExpectedType = true;
834 Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#1-2");
838 Assert.IsTrue (foundTypeOfK, "#1-3");
839 if (mb.LocalVariables.Count < 2)
840 Assert.Ignore ("Code built in release mode - 'T var0' optmized out");
842 Assert.IsTrue (foundExpectedType, "#1-4");
844 foundTypeOfK = false;
845 foundExpectedType = false;
846 mb = typeof (GenericClass<int>).GetMethod("Method2").GetMethodBody();
847 foreach (LocalVariableInfo lvi in mb.LocalVariables) {
848 if (lvi.LocalType == typeofK) {
850 Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#2-1");
851 } else if (lvi.LocalType == typeof (int)) {
852 foundExpectedType = true;
856 Assert.IsTrue (foundTypeOfK, "#2-3");
857 if (mb.LocalVariables.Count < 2)
858 Assert.Ignore ("Code built in release mode - 'int var0' optmized out");
860 Assert.IsTrue (foundExpectedType, "#2-4");
866 class RefOnlyMethodClass
868 // Helper static method
869 static void RefOnlyMethod ()
874 public class MethodHandleTest<T>
876 private List<T> _myList = new List<T> ();
878 public MethodHandleTest ()
880 _myList.Add (default (T));
883 public List<T> MyList {
884 get { return _myList; }
885 set { _myList = value; }