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.Reflection;
33 using System.Runtime.InteropServices;
34 using System.Runtime.CompilerServices;
37 using System.Collections.Generic;
40 namespace MonoTests.System.Reflection
43 public class MethodInfoTest
46 [DllImport ("libfoo", EntryPoint="foo", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
47 public static extern void dllImportMethod ();
49 [MethodImplAttribute(MethodImplOptions.PreserveSig)]
50 public void preserveSigMethod () {
53 [MethodImplAttribute(MethodImplOptions.Synchronized)]
54 public void synchronizedMethod () {
59 [Category ("NotWorking")] // Needs merge of attribute code into gmcs
60 public void PseudoCustomAttributes ()
62 Type t = typeof (MethodInfoTest);
64 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
66 Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
67 Assert.AreEqual ("foo", attr.EntryPoint, "#2");
68 Assert.AreEqual ("libfoo", attr.Value, "#3");
69 Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
70 Assert.AreEqual (false, attr.ExactSpelling, "#5");
71 Assert.AreEqual (true, attr.PreserveSig, "#6");
72 Assert.AreEqual (true, attr.SetLastError, "#7");
73 Assert.AreEqual (true, attr.BestFitMapping, "#8");
74 Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
76 PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
78 // This doesn't work under MS.NET
80 MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
84 [return: MarshalAs (UnmanagedType.Interface)]
85 public void ReturnTypeMarshalAs () {
89 public void ReturnTypePseudoCustomAttributes () {
90 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
92 Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
96 public static int foo (int i, int j)
102 public void StaticInvokeWithObject ()
104 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
106 mi.Invoke (new Object (), new object [] { 1, 2 });
110 public void ByRefInvoke ()
112 MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
113 object[] parms = new object[] {1};
114 met.Invoke (null, parms);
115 Assert.AreEqual (2, parms[0]);
118 public static void ByRefTest (ref int a1)
124 static int byref_arg;
126 public static void ByrefVtype (ref int i) {
133 [Category ("NotDotNet")] // #A2 fails on MS.NET 1.x
135 public void ByrefVtypeInvoke ()
137 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
140 object[] args = new object [] { o };
141 mi.Invoke (null, args);
142 Assert.AreEqual (1, byref_arg, "#A1");
143 Assert.AreEqual (1, o, "#A2");
144 Assert.AreEqual (5, args[0], "#A3");
147 mi.Invoke (null, args);
148 Assert.AreEqual (0, byref_arg, "#B1");
149 Assert.AreEqual (5, args[0], "#B2");
152 public void HeyHey (out string out1, ref string ref1)
158 public void ToStringByRef ()
160 Assert.AreEqual ("Void HeyHey(System.String ByRef, System.String ByRef)",
161 this.GetType ().GetMethod ("HeyHey").ToString ());
164 class GBD_A { public virtual void f () {} }
165 class GBD_B : GBD_A { public override void f () {} }
166 class GBD_C : GBD_B { public override void f () {} }
167 class GBD_D : GBD_C { public new virtual void f () {} }
168 class GBD_E : GBD_D { public override void f () {} }
171 public void GetBaseDefinition ()
173 Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
174 Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
175 Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
180 public void GetMethodBody_Abstract () {
181 MethodBody mb = typeof (ICloneable).GetMethod ("Clone").GetMethodBody ();
186 public void GetMethodBody_Runtime () {
187 MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
192 public void GetMethodBody_Pinvoke () {
193 MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
198 public void GetMethodBody_Icall () {
199 foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
200 if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
201 MethodBody mb = mi.GetMethodBody ();
206 public static void locals_method () {
207 byte[] b = new byte [10];
210 /* This generates a pinned local */
211 fixed (byte *p = &b [0]) {
217 public void GetMethodBody () {
218 MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
220 Assert.IsTrue (mb.InitLocals, "#1");
221 Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
223 IList<LocalVariableInfo> locals = mb.LocalVariables;
225 // This might break with different compilers etc.
226 Assert.AreEqual (2, locals.Count, "#3");
228 Assert.IsTrue ((locals [0].LocalType == typeof (byte[])) || (locals [1].LocalType == typeof (byte[])), "#4");
229 if (locals [0].LocalType == typeof (byte[]))
230 Assert.AreEqual (false, locals [0].IsPinned, "#5");
232 Assert.AreEqual (false, locals [1].IsPinned, "#6");
235 public int return_parameter_test () {
240 public void ReturnParameter () {
241 ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
243 Assert.AreEqual (typeof (int), pi.ParameterType);
244 Assert.AreEqual (-1, pi.Position);
246 //Assert.AreEqual (True, pi.IsRetval);
250 [ExpectedException (typeof (InvalidOperationException))]
251 public void InvokeOnRefOnlyAssembly ()
253 Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
254 Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
255 MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
257 m.Invoke (null, new object [0]);
261 public void InvokeGenericVtype ()
263 KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
264 Type type = kvp.GetType ();
265 Type [] arguments = type.GetGenericArguments ();
266 MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
267 MethodInfo generic_method = method.MakeGenericMethod (arguments);
268 kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
270 Assert.AreEqual ("a", kvp.Key);
271 Assert.AreEqual (21, kvp.Value);
274 public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
279 public void MakeGenericMethodArgsMismatchFoo<T> () {}
282 [ExpectedException (typeof (ArgumentException))]
283 public void MakeGenericMethodArgsMismatch ()
285 MethodInfo gmi = this.GetType ().GetMethod (
286 "MakeGenericMethodArgsMismatchFoo")
287 .MakeGenericMethod ();
290 public static int? pass_nullable (int? i)
296 public void NullableTests ()
298 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
299 Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
300 Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
303 public static void foo_generic<T> () {
307 public void IsGenericMethod ()
309 MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
310 Assert.AreEqual (true, mi.IsGenericMethod, "#1");
311 MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
312 Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
314 MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
315 Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
320 public static void Foo<T2> (T2 i) {
323 public static void Bar () {
327 public static void Baz () {
333 public void ContainsGenericParameters ()
335 // Non-generic method in open generic type
336 Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
337 // open generic method in closed generic type
338 Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
339 // non-generic method in closed generic type
340 Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
341 // closed generic method in closed generic type
342 Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
343 // non-generic method in non-generic nested type of closed generic type
344 Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
345 // non-generic method in non-generic nested type of open generic type
346 Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
349 class GenericHelper<T>
351 public void Test (T t)
359 class RefOnlyMethodClass
361 // Helper static method
362 static void RefOnlyMethod ()