2 // System.Reflection.MethodBase Test Cases
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // Copyright (C) 2008 Gert Driesen
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Reflection;
32 using NUnit.Framework;
34 namespace MonoTests.System.Reflection
36 public class Generic<T> {
40 public void GenericFoo<K> () {
45 public class AnotherGeneric<T> {
50 public class SimpleClass {
51 public void GenericFoo<K> () {
56 class MethodBaseOverloadTestDoesNotCauseCompilerError : MethodBase
58 public override MethodAttributes Attributes
60 get { throw new NotImplementedException (); }
63 public override MethodImplAttributes GetMethodImplementationFlags ()
65 throw new NotImplementedException ();
68 public override ParameterInfo[] GetParameters ()
70 throw new NotImplementedException ();
73 public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, global::System.Globalization.CultureInfo culture)
75 throw new NotImplementedException ();
78 public override RuntimeMethodHandle MethodHandle
80 get { throw new NotImplementedException (); }
83 public override Type DeclaringType
85 get { throw new NotImplementedException (); }
88 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
90 throw new NotImplementedException ();
93 public override object[] GetCustomAttributes (bool inherit)
95 throw new NotImplementedException ();
98 public override bool IsDefined (Type attributeType, bool inherit)
100 throw new NotImplementedException ();
103 public override MemberTypes MemberType
105 get { throw new NotImplementedException (); }
108 public override string Name
110 get { throw new NotImplementedException (); }
113 public override Type ReflectedType
115 get { throw new NotImplementedException (); }
120 public class MethodBaseTest
122 public static MethodInfo Where<T> (T a) {
123 return (MethodInfo) MethodBase.GetCurrentMethod ();
128 public static MethodInfo Where<T> (T a, K b) {
129 return (MethodInfo) MethodBase.GetCurrentMethod ();
134 public void GetCurrentMethodDropsAllGenericArguments ()
136 MethodInfo a = Where<int> (10);
137 MethodInfo b = Foo<int>.Where <double> (10, 10);
139 Assert.IsTrue (a.IsGenericMethodDefinition, "#1");
140 Assert.IsTrue (b.IsGenericMethodDefinition, "#2");
142 Assert.IsTrue (b.DeclaringType.IsGenericTypeDefinition, "#3");
144 Assert.AreSame (a, typeof (MethodBaseTest).GetMethod ("Where"), "#4");
145 Assert.AreSame (b, typeof (Foo<>).GetMethod ("Where"), "#5");
148 [Test] // GetMethodFromHandle (RuntimeMethodHandle)
149 public void GetMethodFromHandle1_Handle_Generic ()
151 G<string> instance = new G<string> ();
152 Type t = instance.GetType ();
153 MethodBase mb1 = t.GetMethod ("M");
154 RuntimeMethodHandle mh = mb1.MethodHandle;
155 RuntimeTypeHandle th = t.TypeHandle;
158 MethodBase.GetMethodFromHandle (mh);
160 } catch (ArgumentException ex) {
161 // Cannot resolve method Void M(System.__Canon)
162 // because the declaring type of the method
163 // handle MonoTests.System.Reflection.MethodBaseTest+G`1[T]
164 // is generic. Explicitly provide the declaring type to
165 // GetMethodFromHandle
169 [Test] // GetMethodFromHandle (RuntimeMethodHandle)
170 public void GetMethodFromHandle1_Handle_Zero ()
172 RuntimeMethodHandle mh = new RuntimeMethodHandle ();
175 MethodBase.GetMethodFromHandle (mh);
177 } catch (ArgumentException ex) {
178 // Handle is not initialized
179 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
180 Assert.IsNull (ex.InnerException, "#3");
181 Assert.IsNotNull (ex.Message, "#4");
182 Assert.IsNull (ex.ParamName, "#5");
187 public void GetMethodFromHandle ()
189 Type t = typeof (object);
190 RuntimeMethodHandle rmh = t.GetConstructor (Type.EmptyTypes).MethodHandle;
191 MethodBase mb = MethodBase.GetMethodFromHandle (rmh);
192 Assert.IsNotNull (mb, "#1");
193 Assert.AreEqual (t, mb.DeclaringType, "#2");
194 Assert.AreEqual (".ctor", mb.Name, "#3");
195 ParameterInfo [] parameters = mb.GetParameters ();
196 Assert.IsNotNull (parameters, "#4");
197 Assert.AreEqual (0, parameters.Length, "#5");
201 public void GetMethodFromHandle_NonGenericType_DeclaringTypeZero ()
203 Type t = typeof (object);
204 RuntimeMethodHandle rmh = t.GetConstructor (Type.EmptyTypes).MethodHandle;
205 MethodBase mb = MethodBase.GetMethodFromHandle (rmh, new RuntimeTypeHandle ());
206 Assert.IsNotNull (mb, "#1");
207 Assert.AreEqual (t, mb.DeclaringType, "#2");
208 Assert.AreEqual (".ctor", mb.Name, "#3");
209 ParameterInfo [] parameters = mb.GetParameters ();
210 Assert.IsNotNull (parameters, "#4");
211 Assert.AreEqual (0, parameters.Length, "#5");
214 [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
215 public void GetMethodFromHandle2_DeclaringType_Zero ()
217 RuntimeTypeHandle th = new RuntimeTypeHandle ();
218 Type t = typeof (G<>);
219 RuntimeMethodHandle mh = t.GetMethod ("M").MethodHandle;
221 MethodBase mb = MethodBase.GetMethodFromHandle (mh, th);
222 Assert.IsNotNull (mb, "#1");
223 Assert.AreEqual (t, mb.DeclaringType, "#2");
224 Assert.AreEqual ("M", mb.Name, "#3");
225 ParameterInfo [] parameters = mb.GetParameters ();
226 Assert.IsNotNull (parameters, "#4");
227 Assert.AreEqual (1, parameters.Length, "#5");
228 Assert.AreEqual (t.GetGenericArguments () [0] , parameters [0].ParameterType, "#6");
231 [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
232 public void GetMethodFromHandle2_Handle_Generic ()
234 G<string> instance = new G<string> ();
235 Type t = instance.GetType ();
236 MethodBase mb1 = t.GetMethod ("M");
237 RuntimeMethodHandle mh = mb1.MethodHandle;
238 RuntimeTypeHandle th = t.TypeHandle;
240 MethodBase mb2 = MethodBase.GetMethodFromHandle (mh, th);
241 Assert.IsNotNull (mb2, "#1");
242 Assert.AreEqual (t, mb2.DeclaringType, "#2");
243 Assert.AreEqual ("M", mb2.Name, "#3");
244 ParameterInfo [] parameters = mb2.GetParameters ();
245 Assert.IsNotNull (parameters, "#4");
246 Assert.AreEqual (1, parameters.Length, "#5");
247 Assert.AreEqual (typeof (string), parameters [0].ParameterType, "#6");
250 [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
251 public void GetMethodFromHandle2_Handle_Zero ()
253 RuntimeTypeHandle th = typeof (G<>).TypeHandle;
254 RuntimeMethodHandle mh = new RuntimeMethodHandle ();
257 MethodBase.GetMethodFromHandle (mh, th);
259 } catch (ArgumentException ex) {
260 // Handle is not initialized
261 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
262 Assert.IsNull (ex.InnerException, "#3");
263 Assert.IsNotNull (ex.Message, "#4");
264 Assert.IsNull (ex.ParamName, "#5");
276 public void GetMethodFromHandle_Handle_Generic_Method ()
278 MethodInfo mi = typeof (SimpleClass).GetMethod ("GenericFoo");
279 RuntimeMethodHandle handle = mi.MethodHandle;
281 MethodBase res = MethodBase.GetMethodFromHandle (handle);
282 Assert.AreEqual (mi, res, "#1");
284 res = MethodBase.GetMethodFromHandle (handle, typeof (SimpleClass).TypeHandle);
285 Assert.AreEqual (mi, res, "#2");
290 public void GetMethodFromHandle_Handle_Generic_Method_Instance ()
292 MethodInfo mi = typeof (SimpleClass).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
293 RuntimeMethodHandle handle = mi.MethodHandle;
295 MethodBase res = MethodBase.GetMethodFromHandle (handle);
296 Assert.AreEqual (mi, res, "#1");
298 res = MethodBase.GetMethodFromHandle (handle, typeof (SimpleClass).TypeHandle);
299 Assert.AreEqual (mi, res, "#2");
303 public void GetMethodFromHandle_Handle_Generic_Method_On_Generic_Class ()
305 MethodInfo mi = typeof (Generic<>).GetMethod ("GenericFoo");
306 RuntimeMethodHandle handle = mi.MethodHandle;
310 MethodBase.GetMethodFromHandle (handle);
312 } catch (ArgumentException) {
315 mi = typeof (Generic<int>).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
316 handle = mi.MethodHandle;
319 MethodBase.GetMethodFromHandle (handle);
321 } catch (ArgumentException) {
325 mi = typeof (Generic<>).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
326 handle = mi.MethodHandle;
329 MethodBase.GetMethodFromHandle (handle);
331 } catch (ArgumentException) {
335 res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<int>).TypeHandle);
336 Assert.AreEqual (typeof (Generic<int>), res.DeclaringType, "#4");
338 res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<double>).TypeHandle);
339 Assert.AreEqual (typeof (Generic<double>), res.DeclaringType, "#5");
341 res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
342 Assert.AreEqual (typeof (Generic<>), res.DeclaringType, "#6");
345 MethodBase.GetMethodFromHandle(handle, typeof (AnotherGeneric<double>).TypeHandle);
347 } catch (ArgumentException) {
352 public void GetMethodFromHandle_Handle_Method_On_Generic_Class ()
354 MethodInfo mi = typeof (Generic<>).GetMethod ("Foo");
355 RuntimeMethodHandle handle = mi.MethodHandle;
359 MethodBase.GetMethodFromHandle(handle);
361 } catch (ArgumentException) {
364 res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
365 Assert.AreEqual (res, mi, "#2");
367 mi = typeof (Generic<int>).GetMethod ("Foo");
368 handle = mi.MethodHandle;
371 MethodBase.GetMethodFromHandle(handle);
373 } catch (ArgumentException) {
376 res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<int>).TypeHandle);
377 Assert.AreEqual (typeof (Generic<int>), res.DeclaringType, "#4");
379 res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<double>).TypeHandle);
380 Assert.AreEqual (typeof (Generic<double>), res.DeclaringType, "#5");
382 res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
383 Assert.AreEqual (typeof (Generic<>), res.DeclaringType, "#6");
386 MethodBase.GetMethodFromHandle(handle, typeof (AnotherGeneric<double>).TypeHandle);
388 } catch (ArgumentException) {
392 // test case adapted from http://www.chrishowie.com/2010/11/24/mutable-strings-in-mono/
393 public class FakeString {
395 public char start_char;
398 private static FakeString UnsafeConversion<T> (T thing) where T : FakeString
405 [Category ("NotWorking")] // #10552
407 public void MutableString ()
409 var m = typeof (MethodBaseTest).GetMethod ("UnsafeConversion", BindingFlags.NonPublic | BindingFlags.Static);
411 var m2 = m.MakeGenericMethod (typeof (string));
412 Assert.Fail ("MakeGenericMethod");
414 catch (ArgumentException) {