| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- //
- // System.Reflection.MethodInfo Test Cases
- //
- // Authors:
- // Zoltan Varga ([email protected])
- //
- // (c) 2003 Ximian, Inc. (http://www.ximian.com)
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using NUnit.Framework;
- using System;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Runtime.CompilerServices;
- #if NET_2_0
- using System.Collections.Generic;
- #endif
- namespace MonoTests.System.Reflection
- {
- [TestFixture]
- public class MethodInfoTest
- {
- [DllImport ("libfoo", EntryPoint="foo", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
- public static extern void dllImportMethod ();
- [MethodImplAttribute(MethodImplOptions.PreserveSig)]
- public void preserveSigMethod () {
- }
- [MethodImplAttribute(MethodImplOptions.Synchronized)]
- public void synchronizedMethod () {
- }
- #if NET_2_0
- [Test]
- [Category ("NotWorking")] // Needs merge of attribute code into gmcs
- public void PseudoCustomAttributes ()
- {
- Type t = typeof (MethodInfoTest);
- DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
- Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
- Assert.AreEqual ("foo", attr.EntryPoint, "#2");
- Assert.AreEqual ("libfoo", attr.Value, "#3");
- Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
- Assert.AreEqual (false, attr.ExactSpelling, "#5");
- Assert.AreEqual (true, attr.PreserveSig, "#6");
- Assert.AreEqual (true, attr.SetLastError, "#7");
- Assert.AreEqual (true, attr.BestFitMapping, "#8");
- Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
- PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
- // This doesn't work under MS.NET
- /*
- MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
- */
- }
- [return: MarshalAs (UnmanagedType.Interface)]
- public void ReturnTypeMarshalAs () {
- }
- [Test]
- public void ReturnTypePseudoCustomAttributes () {
- MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
- Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
- }
- #endif
- public static int foo (int i, int j)
- {
- return i + j;
- }
- [Test]
- public void StaticInvokeWithObject ()
- {
- MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
-
- mi.Invoke (new Object (), new object [] { 1, 2 });
- }
- [Test]
- public void ByRefInvoke ()
- {
- MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
- object[] parms = new object[] {1};
- met.Invoke (null, parms);
- Assert.AreEqual (2, parms[0]);
- }
- public static void ByRefTest (ref int a1)
- {
- if (a1 == 1)
- a1 = 2;
- }
- static int byref_arg;
- public static void ByrefVtype (ref int i) {
- byref_arg = i;
- i = 5;
- }
- [Test]
- #if ONLY_1_1
- [Category ("NotDotNet")] // #A2 fails on MS.NET 1.x
- #endif
- public void ByrefVtypeInvoke ()
- {
- MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
- object o = 1;
- object[] args = new object [] { o };
- mi.Invoke (null, args);
- Assert.AreEqual (1, byref_arg, "#A1");
- Assert.AreEqual (1, o, "#A2");
- Assert.AreEqual (5, args[0], "#A3");
- args [0] = null;
- mi.Invoke (null, args);
- Assert.AreEqual (0, byref_arg, "#B1");
- Assert.AreEqual (5, args[0], "#B2");
- }
- public void HeyHey (out string out1, ref string ref1)
- {
- out1 = null;
- }
- [Test] // bug #76541
- public void ToStringByRef ()
- {
- Assert.AreEqual ("Void HeyHey(System.String ByRef, System.String ByRef)",
- this.GetType ().GetMethod ("HeyHey").ToString ());
- }
- #if NET_2_0
- [Test]
- public void GetMethodBody_Abstract () {
- MethodBody mb = typeof (ICloneable).GetMethod ("Clone").GetMethodBody ();
- Assert.IsNull (mb);
- }
- [Test]
- public void GetMethodBody_Runtime () {
- MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
- Assert.IsNull (mb);
- }
- [Test]
- public void GetMethodBody_Pinvoke () {
- MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
- Assert.IsNull (mb);
- }
- [Test]
- public void GetMethodBody_Icall () {
- foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
- if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
- MethodBody mb = mi.GetMethodBody ();
- Assert.IsNull (mb);
- }
- }
- public static void locals_method () {
- byte[] b = new byte [10];
- unsafe {
- /* This generates a pinned local */
- fixed (byte *p = &b [0]) {
- }
- }
- }
- [Test]
- public void GetMethodBody () {
- MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
- Assert.IsTrue (mb.InitLocals, "#1");
- Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
- IList<LocalVariableInfo> locals = mb.LocalVariables;
- // This might break with different compilers etc.
- Assert.AreEqual (2, locals.Count, "#3");
- Assert.IsTrue ((locals [0].LocalType == typeof (byte[])) || (locals [1].LocalType == typeof (byte[])), "#4");
- if (locals [0].LocalType == typeof (byte[]))
- Assert.AreEqual (false, locals [0].IsPinned, "#5");
- else
- Assert.AreEqual (false, locals [1].IsPinned, "#6");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void InvokeOnRefOnlyAssembly ()
- {
- Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
- Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
- MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
-
- m.Invoke (null, new object [0]);
- }
- public void MakeGenericMethodArgsMismatchFoo<T> () {}
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void MakeGenericMethodArgsMismatch ()
- {
- MethodInfo gmi = this.GetType ().GetMethod (
- "MakeGenericMethodArgsMismatchFoo")
- .MakeGenericMethod ();
- }
- public static int? pass_nullable (int? i)
- {
- return i;
- }
- [Test]
- public void NullableTests ()
- {
- MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
- Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
- Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
- }
- public static void foo_generic<T> () {
- }
- [Test]
- public void IsGenericMethod ()
- {
- MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
- Assert.AreEqual (true, mi.IsGenericMethod, "#1");
- MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
- Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
- MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
- Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
- }
- class GenericHelper<T>
- {
- public void Test (T t)
- { }
- }
- #endif
- }
-
- #if NET_2_0
- // Helper class
- class RefOnlyMethodClass
- {
- // Helper static method
- static void RefOnlyMethod ()
- {
- }
- }
- #endif
- }
|