Files
acceptance-tests
data
debian
docs
external
ikvm-native
libgc
llvm
m4
man
mcs
build
class
Accessibility
Commons.Xml.Relaxng
Cscompmgd
CustomMarshalers
Facades
I18N
IBM.Data.DB2
ICSharpCode.SharpZipLib
Microsoft.Build
Microsoft.Build.Engine
Microsoft.Build.Framework
Microsoft.Build.Tasks
Microsoft.Build.Utilities
Microsoft.CSharp
Microsoft.NuGet.Build.Tasks
Microsoft.VisualC
Microsoft.Web.Infrastructure
MicrosoftAjaxLibrary
Mono.Btls.Interface
Mono.C5
Mono.CSharp
Mono.Cairo
Mono.Cecil
Mono.Cecil.Mdb
Mono.CodeContracts
Mono.CompilerServices.SymbolWriter
Mono.Data.Sqlite
Mono.Data.Tds
Mono.Debugger.Soft
Mono.Http
Mono.Management
Mono.Messaging
Mono.Messaging.RabbitMQ
Mono.Options
Mono.Parallel
Mono.Posix
Mono.Profiler.Log
Mono.Runtime.Tests
Mono.Security
Mono.Security.Win32
Mono.Simd
Mono.Tasklets
Mono.WebBrowser
Mono.XBuild.Tasks
Novell.Directory.Ldap
PEAPI
RabbitMQ.Client
SMDiagnostics
System
System.ComponentModel.Composition.4.5
System.ComponentModel.DataAnnotations
System.Configuration
System.Configuration.Install
System.Core
System.Data
System.Data.DataSetExtensions
System.Data.Entity
System.Data.Linq
System.Data.OracleClient
System.Data.Services
System.Data.Services.Client
System.Deployment
System.Design
System.DirectoryServices
System.DirectoryServices.Protocols
System.Drawing
System.Drawing.Design
System.Dynamic
System.EnterpriseServices
System.IO.Compression
System.IO.Compression.FileSystem
System.IdentityModel
System.IdentityModel.Selectors
System.Json
System.Json.Microsoft
System.Management
System.Messaging
System.Net
System.Net.Http
System.Net.Http.Formatting
System.Net.Http.WebRequest
System.Net.Http.WinHttpHandler
System.Numerics
System.Numerics.Vectors
System.Reactive.Core
System.Reactive.Debugger
System.Reactive.Experimental
System.Reactive.Interfaces
System.Reactive.Linq
System.Reactive.Observable.Aliases
System.Reactive.PlatformServices
System.Reactive.Providers
System.Reactive.Runtime.Remoting
System.Reactive.Windows.Forms
System.Reactive.Windows.Threading
System.Reflection.Context
System.Runtime.Caching
System.Runtime.CompilerServices.Unsafe
System.Runtime.DurableInstancing
System.Runtime.Remoting
System.Runtime.Serialization
System.Runtime.Serialization.Formatters.Soap
System.Security
System.ServiceModel
System.ServiceModel.Activation
System.ServiceModel.Discovery
System.ServiceModel.Internals
System.ServiceModel.Routing
System.ServiceModel.Web
System.ServiceProcess
System.Threading.Tasks.Dataflow
System.Transactions
System.Web
System.Web.Abstractions
System.Web.ApplicationServices
System.Web.DynamicData
System.Web.Extensions
System.Web.Extensions.Design
System.Web.Http
System.Web.Http.SelfHost
System.Web.Http.WebHost
System.Web.Mobile
System.Web.Mvc3
System.Web.Razor
System.Web.RegularExpressions
System.Web.Routing
System.Web.Services
System.Web.WebPages
System.Web.WebPages.Deployment
System.Web.WebPages.Razor
System.Windows
System.Windows.Forms
System.Windows.Forms.DataVisualization
System.Workflow.Activities
System.Workflow.ComponentModel
System.Workflow.Runtime
System.XML
System.Xaml
System.Xml.Linq
System.Xml.Serialization
SystemWebTestShim
WebMatrix.Data
WindowsBase
aot-compiler
corlib
Assembly
CommonCrypto
CoreFoundation
Documentation
LinkerDescriptor
Microsoft.Win32
Mono
Mono.Globalization.Unicode
Mono.Interop
Mono.Security
Mono.Security.Cryptography
Mono.Xml
ReferenceSources
System
System.Configuration.Assemblies
System.Deployment.Internal
System.Diagnostics
System.Diagnostics.SymbolStore
System.Diagnostics.Tracing
System.Globalization
System.IO
System.IO.IsolatedStorage
System.Reflection
System.Reflection.Emit
System.Reflection.Metadata
System.Resources
System.Runtime
System.Runtime.CompilerServices
System.Runtime.Hosting
System.Runtime.InteropServices
System.Runtime.InteropServices.RuntimeInformation
System.Runtime.InteropServices.WindowsRuntime
System.Runtime.Remoting
System.Runtime.Remoting.Activation
System.Runtime.Remoting.Channels
System.Runtime.Remoting.Contexts
System.Runtime.Remoting.Lifetime
System.Runtime.Remoting.Messaging
System.Runtime.Remoting.Metadata
System.Runtime.Remoting.Metadata.W3cXsd2001
System.Runtime.Remoting.Proxies
System.Runtime.Remoting.Services
System.Runtime.Versioning
System.Security
System.Security.AccessControl
System.Security.Cryptography
System.Security.Cryptography.X509Certificates
System.Security.Permissions
System.Security.Policy
System.Security.Principal
System.Text
System.Threading
System.Threading.Tasks
Test
Microsoft.Win32
Mono
System
System.Collections
System.Collections.Concurrent
System.Collections.Generic
System.Collections.ObjectModel
System.Diagnostics
System.Diagnostics.Contracts
System.Globalization
System.IO
System.IO.IsolatedStorage
System.Reflection
AssemblyAlgorithmIdAttributeTest.cs
AssemblyCas.cs
AssemblyCompanyAttributeTest.cs
AssemblyConfigurationAttributeTest.cs
AssemblyCopyrightAttributeTest.cs
AssemblyCultureAttributeTest.cs
AssemblyDefaultAliasAttributeTest.cs
AssemblyDelaySignAttributeTest.cs
AssemblyDescriptionAttributeTest.cs
AssemblyFileVersionAttributeTest.cs
AssemblyFlagsAttributeTest.cs
AssemblyInformationalVersionAttributeTest.cs
AssemblyKeyFileAttributeTest.cs
AssemblyKeyNameAttributeTest.cs
AssemblyMetadataAttributeTest.cs
AssemblyNameCas.cs
AssemblyNameTest.cs
AssemblyProductAttributeTest.cs
AssemblySignatureKeyAttributeTest.cs
AssemblyTest.cs
AssemblyTitleAttributeTest.cs
AssemblyTrademarkAttributeTest.cs
AssemblyVersionAttributeTest.cs
BinderTests.cs
ConstructorInfoTest.cs
CustomAttributeDataTest.cs
EventInfoTest.cs
FieldInfoTest.cs
IntrospectionExtensionsTest.cs
MethodBaseTest.cs
MethodInfoTest.cs
ModuleCas.cs
ModuleTest.cs
MonoGenericClassTest.cs
ParameterInfoTest.cs
PropertyInfoTest.cs
ReflectedTypeTest.cs
StrongNameKeyPairCas.cs
StrongNameKeyPairTest.cs
TypeDelegatorTest.cs
VisibilityTest.cs
System.Reflection.Emit
System.Resources
System.Runtime.CompilerServices
System.Runtime.ExceptionServices
System.Runtime.InteropServices
System.Runtime.Remoting
System.Runtime.Remoting.Channels
System.Runtime.Remoting.Contexts
System.Runtime.Remoting.Messaging
System.Runtime.Remoting.Metadata.W3cXsd2001
System.Runtime.Remoting.Proxies
System.Runtime.Serialization
System.Runtime.Serialization.Formatters.Binary
System.Runtime.Versioning
System.Security
System.Security.AccessControl
System.Security.Claims
System.Security.Cryptography
System.Security.Cryptography.X509Certificates
System.Security.Permissions
System.Security.Policy
System.Security.Principal
System.Text
System.Threading
System.Threading.Tasks
resources
coreclr
corefx
corert
il
resources
Makefile
corlib.dll.sources
corlib_test.dll.sources
darwin_net_4_x_corlib.dll.sources
darwin_xammac_net_4_5_corlib.dll.exclude.sources
linux_net_4_x_corlib.dll.sources
monotouch_corlib.dll.exclude.sources
monotouch_corlib.dll.sources
monotouch_runtime_corlib.dll.exclude.sources
monotouch_runtime_corlib.dll.sources
monotouch_tv_corlib.dll.exclude.sources
monotouch_tv_corlib.dll.sources
monotouch_tv_runtime_corlib.dll.exclude.sources
monotouch_tv_runtime_corlib.dll.sources
monotouch_watch_corlib.dll.exclude.sources
monotouch_watch_corlib.dll.sources
monotouch_watch_runtime_corlib.dll.exclude.sources
monotouch_watch_runtime_corlib.dll.sources
net_4_x_corlib.dll.sources
win32_net_4_x_corlib.dll.exclude.sources
win32_net_4_x_corlib.dll.sources
winaot_corlib.dll.exclude.sources
winaot_corlib.dll.sources
xammac_corlib.dll.exclude.sources
xammac_corlib.dll.sources
dlr
doc
legacy
lib
monodoc
notes
reference-assemblies
referencesource
test-helpers
LICENSE
Makefile
Open.snk
README
ecma.pub
mono.pub
mono.snk
msfinal.pub
reactive.pub
silverlight.pub
winfx.pub
winfx3.pub
docs
errors
ilasm
jay
mcs
nunit24
packages
tests
tools
AUTHORS
COPYING
INSTALL.txt
Makefile
MonoIcon.png
README
ScalableMonoIcon.svg
mkinstalldirs
mk
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/mcs/class/corlib/Test/System.Reflection/MethodInfoTest.cs

1023 lines
32 KiB
C#
Raw Normal View History

//
// System.Reflection.MethodInfo Test Cases
//
// Authors:
// Zoltan Varga (vargaz@gmail.com)
// Aleksey Kliger (aleksey@xamarin.com)
//
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
// Copyright (C) 2015 Xamarin, Inc. (http://www.xamarin.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.Linq;
using System.Threading;
using System.Reflection;
#if !MONOTOUCH && !FULL_AOT_RUNTIME
using System.Reflection.Emit;
#endif
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
namespace A.B.C {
// Disable expected warning
#pragma warning disable 169
public struct MethodInfoTestStruct {
int p;
}
#pragma warning restore 169
}
namespace MonoTests.System.Reflection
{
[TestFixture]
public class MethodInfoTest
{
#if MONOTOUCH || FULL_AOT_RUNTIME
// use an existing symbol - so we can build without dlsym. It does not matter that the signature does not match for the test
[DllImport ("libc", EntryPoint="readlink", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
#else
[DllImport ("libfoo", EntryPoint="foo", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
#endif
public static extern void dllImportMethod ();
[MethodImplAttribute(MethodImplOptions.PreserveSig)]
public void preserveSigMethod ()
{
}
[MethodImplAttribute(MethodImplOptions.Synchronized)]
public void synchronizedMethod ()
{
}
public interface InterfaceTest
{
void Clone ();
}
[Test]
public void IsDefined_AttributeType_Null ()
{
MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
try {
mi.IsDefined ((Type) null, false);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNotNull (ex.ParamName, "#5");
Assert.AreEqual ("attributeType", ex.ParamName, "#6");
}
}
[Test]
public void TestInvokeByRefReturnMethod ()
{
try {
MethodInfo m = typeof (int[]).GetMethod ("Address");
m.Invoke (new int[1], new object[] { 0 });
Assert.Fail ("#1");
} catch (NotSupportedException e) {
Assert.AreEqual (typeof (NotSupportedException), e.GetType (), "#2");
Assert.IsNull (e.InnerException, "#3");
Assert.IsNotNull (e.Message, "#4");
}
}
[Test]
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");
#if MONOTOUCH || FULL_AOT_RUNTIME
Assert.AreEqual ("readlink", attr.EntryPoint, "#2");
Assert.AreEqual ("libc", attr.Value, "#3");
#else
Assert.AreEqual ("foo", attr.EntryPoint, "#2");
Assert.AreEqual ("libfoo", attr.Value, "#3");
#endif
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);
}
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]
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 DateTime ref1)
{
out1 = null;
}
public void SignatureTest (__arglist)
{
}
public static unsafe int* PtrFunc (int* a)
{
return (int*) 0;
}
#if MONO_FEATURE_THREAD_ABORT
[Test] // bug #81538
public void InvokeThreadAbort ()
{
MethodInfo method = typeof (MethodInfoTest).GetMethod ("AbortIt");
try {
method.Invoke (null, new object [0]);
Assert.Fail ("#1");
}
catch (ThreadAbortException ex) {
Thread.ResetAbort ();
Assert.IsNull (ex.InnerException, "#2");
}
}
public static void AbortIt ()
{
Thread.CurrentThread.Abort ();
}
#endif
[Test] // bug #76541
public void ToStringByRef ()
{
Assert.AreEqual ("Void HeyHey(System.String ByRef, System.DateTime ByRef)",
this.GetType ().GetMethod ("HeyHey").ToString ());
}
[Test]
public void ToStringArgList ()
{
Assert.AreEqual ("Void SignatureTest(...)",
this.GetType ().GetMethod ("SignatureTest").ToString ());
}
[Test]
public void ToStringWithPointerSignatures () //bug #409583
{
Assert.AreEqual ("Int32* PtrFunc(Int32*)", this.GetType ().GetMethod ("PtrFunc").ToString ());
}
public struct SimpleStruct
{
public int a;
}
public static unsafe SimpleStruct* PtrFunc2 (SimpleStruct* a, A.B.C.MethodInfoTestStruct *b)
{
return (SimpleStruct*) 0;
}
[Test]
public void ToStringWithPointerSignaturesToNonPrimitiveType ()
{
Assert.AreEqual ("SimpleStruct* PtrFunc2(SimpleStruct*, A.B.C.MethodInfoTestStruct*)",
this.GetType ().GetMethod ("PtrFunc2").ToString ());
}
[Test]
public void ToStringGenericMethod ()
{
Assert.AreEqual ("System.Collections.ObjectModel.ReadOnlyCollection`1[T] AsReadOnly[T](T[])",
typeof (Array).GetMethod ("AsReadOnly").ToString ());
}
class GBD_A { public virtual void f () {} }
class GBD_B : GBD_A { public override void f () {} }
class GBD_C : GBD_B { public override void f () {} }
class GBD_D : GBD_C { public new virtual void f () {} }
class GBD_E : GBD_D { public override void f () {} }
class GBD_E2 : GBD_D { }
class GBD_F : GBD_E { }
[Test]
public void GetBaseDefinition ()
{
Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().ReflectedType, "#1r");
Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().ReflectedType, "#2r");
Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().ReflectedType, "#3r");
Assert.AreEqual (typeof (GBD_D), typeof (GBD_E2).GetMethod ("f").GetBaseDefinition ().DeclaringType, "#4");
Assert.AreEqual (typeof (GBD_D), typeof (GBD_E2).GetMethod ("f").GetBaseDefinition ().ReflectedType, "#4r");
Assert.AreEqual (typeof (GBD_D), typeof (GBD_F).GetMethod ("f").GetBaseDefinition ().DeclaringType, "#5");
Assert.AreEqual (typeof (GBD_D), typeof (GBD_F).GetMethod ("f").GetBaseDefinition ().ReflectedType, "#5r");
}
class GenericBase<T,H> {
public virtual void f2 () { }
}
class GenericMid<T, U> : GenericBase<T, Action<U>> {
public virtual T f1 () { return default (T); }
}
class GenericChild<T> : GenericMid<T, int> {
public override T f1 () { return default (T); }
public override void f2 () { }
}
class DerivedFromGenericBase : GenericBase<int, int> {
}
[Test]
public void GetBaseDefinition_OpenConstructedBaseType () // 36305
{
var t = typeof (GenericChild<string>);
var mi1 = t.GetMethod ("f1");
var mi1_base = mi1.GetBaseDefinition ();
Assert.AreEqual (typeof (GenericMid<string, int>), mi1_base.DeclaringType, "#1");
var mi2 = t.GetMethod ("f2");
var mi2_base = mi2.GetBaseDefinition ();
Assert.AreEqual (typeof (GenericBase<string, Action<int>>), mi2_base.DeclaringType, "#2");
}
class TestInheritedMethodA {
private void TestMethod ()
{
}
public void TestMethod2 ()
{
}
}
class TestInheritedMethodB : TestInheritedMethodA {
}
[Test]
public void InheritanceTestGetMethodTest ()
{
MethodInfo inheritedMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
MethodInfo baseMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Assert.AreSame (inheritedMethod, baseMethod);
MethodInfo inheritedMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
MethodInfo baseMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Assert.AreSame (inheritedMethod, baseMethod);
}
[Test]
public void GetMethodBody_Abstract ()
{
MethodBody mb = typeof (InterfaceTest).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 ()
{
#if (MONOTOUCH || FULL_AOT_RUNTIME) && !DEBUG
Assert.Ignore ("Release app (on devices) are stripped of (managed) IL so this test would fail");
#endif
MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
Assert.IsTrue (mb.InitLocals, "#1");
Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
IList<LocalVariableInfo> locals = mb.LocalVariables;
bool foundPinnedBytePointer = false;
unsafe {
foreach (LocalVariableInfo lvi in locals) {
if (lvi.LocalType == typeof (byte[]))
// This is optimized out by CSC in .NET 4.6
Assert.IsFalse (lvi.IsPinned, "#3-1");
if (/* mcs */ lvi.LocalType == typeof (byte*) || /* csc */ lvi.LocalType == typeof (byte).MakeByRefType ()) {
// We have three locals. There's b the byte[], there's a byte* and there's a byte&.
// mcs emits a byte* for the latter type.
// We need to find one such pinned byte pointer. Therefore we're folding with logical or
foundPinnedBytePointer = foundPinnedBytePointer || lvi.IsPinned;
}
}
}
Assert.IsTrue (foundPinnedBytePointer, "#4");
}
public int return_parameter_test ()
{
return 0;
}
[Test]
public void GetMethodFromHandle_Generic ()
{
MethodHandleTest<int> test = new MethodHandleTest<int> ();
RuntimeMethodHandle mh = test.GetType ().GetProperty ("MyList")
.GetGetMethod ().MethodHandle;
MethodBase mb = MethodInfo.GetMethodFromHandle (mh,
typeof (MethodHandleTest<int>).TypeHandle);
Assert.IsNotNull (mb, "#1");
List<int> list = (List<int>) mb.Invoke (test, null);
Assert.IsNotNull (list, "#2");
Assert.AreEqual (1, list.Count, "#3");
}
[Test]
public void ReturnParameter ()
{
ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
Assert.AreEqual (typeof (int), pi.ParameterType, "#1");
Assert.AreEqual (-1, pi.Position, "#2");
// MS always return false here
//Assert.IsTrue (pi.IsRetval, "#3");
}
[Test]
public void MethodInfoModule ()
{
Type type = typeof (MethodInfoTest);
MethodInfo me = type.GetMethod ("return_parameter_test");
Assert.AreEqual (type.Module, me.Module);
}
[Test]
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);
try {
m.Invoke (null, new object [0]);
Assert.Fail ("#1");
} catch (InvalidOperationException ex) {
// The requested operation is invalid in the
// ReflectionOnly context
Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
}
}
[Test]
[ExpectedException (typeof (TargetInvocationException))]
public void InvokeInvalidOpExceptionThrow () {
MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ThrowMethod");
mi.Invoke(null, null);
}
public static void ThrowMethod () {
throw new InvalidOperationException ();
}
[Test]
public void InvokeGenericVtype ()
{
KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
Type type = kvp.GetType ();
Type [] arguments = type.GetGenericArguments ();
MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
MethodInfo generic_method = method.MakeGenericMethod (arguments);
kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
Assert.AreEqual ("a", kvp.Key, "#1");
Assert.AreEqual (21, kvp.Value, "#2");
}
public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
{
return kvp;
}
[Test] // bug #81997
public void InvokeGenericInst ()
{
List<string> str = null;
object [] methodArgs = new object [] { str };
MethodInfo mi = typeof (MethodInfoTest).GetMethod ("GenericRefMethod");
mi.Invoke (null, methodArgs);
Assert.IsNotNull (methodArgs [0], "#A1");
Assert.IsNull (str, "#A2");
Assert.IsTrue (methodArgs [0] is List<string>, "#A3");
List<string> refStr = methodArgs [0] as List<string>;
Assert.IsNotNull (refStr, "#B1");
Assert.AreEqual (1, refStr.Count, "#B2");
Assert.AreEqual ("test", refStr [0], "#B3");
}
public static void GenericRefMethod (ref List<string> strArg)
{
strArg = new List<string> ();
strArg.Add ("test");
}
public void MakeGenericMethodArgsMismatchFoo<T> ()
{
}
[Test]
public void MakeGenericMethodArgsMismatch ()
{
MethodInfo gmi = this.GetType ().GetMethod (
"MakeGenericMethodArgsMismatchFoo");
try {
gmi.MakeGenericMethod ();
Assert.Fail ("#1");
} catch (ArgumentException ex) {
// The type or method has 1 generic parameter(s),
// but 0 generic argument(s) were provided. A
// generic argument must be provided for each
// generic parameter
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNull (ex.ParamName, "#5");
}
}
public void SimpleGenericMethod<TFoo, TBar> () {}
[Test]
public void MakeGenericMethodWithNullArray ()
{
MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
try {
gmi.MakeGenericMethod ((Type []) null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("methodInstantiation", ex.ParamName, "#5");
}
}
[Test]
public void MakeGenericMethodWithNullValueInTypesArray ()
{
MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
try {
gmi.MakeGenericMethod (new Type [] { typeof (int), null });
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNull (ex.ParamName, "#5");
}
}
[Test]
public void MakeGenericMethodWithNonGenericMethodDefinitionMethod ()
{
MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
MethodInfo inst = gmi.MakeGenericMethod (typeof (int), typeof (double));
try {
inst.MakeGenericMethod (typeof (int), typeof (double));
Assert.Fail ("#1");
} catch (InvalidOperationException ex) {
}
}
#if !MONOTOUCH && !FULL_AOT_RUNTIME
public TFoo SimpleGenericMethod2<TFoo, TBar> () { return default (TFoo); }
/*Test for the uggly broken behavior of SRE.*/
[Test]
public void MakeGenericMethodWithSreTypeResultsInStupidMethodInfo ()
{
AssemblyName assemblyName = new AssemblyName ();
assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodInfoTest";
AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
ModuleBuilder module = assembly.DefineDynamicModule ("module1", "tst.dll");
TypeBuilder tb = module.DefineType ("Test", TypeAttributes.Public);
MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod2");
MethodInfo ins = gmi.MakeGenericMethod (typeof (int), tb);
Assert.AreSame (tb, ins.GetGenericArguments () [1], "#1");
/*broken ReturnType*/
Assert.AreSame (gmi.GetGenericArguments () [0], ins.ReturnType, "#2");
}
#endif
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");
// Test conversion of vtype to a nullable type for the this argument
PropertyInfo pi = typeof (Nullable <int>).GetProperty ("HasValue");
Assert.AreEqual (true, pi.GetGetMethod ().Invoke (10, null));
PropertyInfo pi2 = typeof (Nullable <int>).GetProperty ("Value");
Assert.AreEqual (10, pi2.GetGetMethod ().Invoke (10, null));
}
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 A<T>
{
public static void Foo<T2> (T2 i)
{
}
public static void Bar ()
{
}
public class B
{
public static void Baz ()
{
}
}
}
[Test]
public void ContainsGenericParameters ()
{
// Non-generic method in open generic type
Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
// open generic method in closed generic type
Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
// non-generic method in closed generic type
Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
// closed generic method in closed generic type
Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
// non-generic method in non-generic nested type of closed generic type
Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
// non-generic method in non-generic nested type of open generic type
Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
}
[Test]
public void IsGenericMethodDefinition ()
{
MethodInfo m1 = typeof (A<>).GetMethod ("Foo");
Assert.IsTrue (m1.IsGenericMethod, "#A1");
Assert.IsTrue (m1.IsGenericMethodDefinition, "#A2");
MethodInfo m2 = typeof (A<int>).GetMethod ("Foo");
Assert.IsTrue (m2.IsGenericMethod, "#B1");
Assert.IsTrue (m2.IsGenericMethodDefinition, "#B2");
MethodInfo m3 = m2.MakeGenericMethod (typeof (int));
Assert.IsTrue (m3.IsGenericMethod, "#C1");
Assert.IsFalse (m3.IsGenericMethodDefinition, "#C2");
}
[Test]
public void GetGenericMethodDefinition ()
{
MethodInfo mi1 = typeof (MyList<>).GetMethod ("ConvertAll");
MethodInfo mi2 = typeof (MyList<int>).GetMethod ("ConvertAll");
Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[T,TOutput]",
mi1.GetParameters () [0].ParameterType.ToString (), "#A1");
Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[System.Int32,TOutput]",
mi2.GetParameters () [0].ParameterType.ToString (), "#A2");
Assert.IsTrue (mi1.IsGenericMethod, "#A3");
Assert.IsTrue (mi1.IsGenericMethodDefinition, "#A4");
Assert.IsTrue (mi2.IsGenericMethod, "#A5");
Assert.IsTrue (mi2.IsGenericMethodDefinition, "#A6");
MethodInfo mi3 = mi2.GetGenericMethodDefinition ();
Assert.IsTrue (mi3.IsGenericMethod, "#B1");
Assert.IsTrue (mi3.IsGenericMethodDefinition, "#B2");
Assert.AreSame (mi2, mi3, "#B3");
MethodInfo mi4 = mi2.MakeGenericMethod (typeof (short));
Assert.IsTrue (mi4.IsGenericMethod, "#C1");
Assert.IsFalse (mi4.IsGenericMethodDefinition, "#C2");
Assert.AreSame (mi2, mi4.GetGenericMethodDefinition (), "#C3");
}
public void TestMethod123(int a, int b) {}
[Test]
public void GetParametersDontReturnInternedArray ()
{
var method = typeof (MethodInfoTest).GetMethod ("TestMethod123");
var parms = method.GetParameters ();
Assert.AreNotSame (parms, method.GetParameters (), "#1");
parms [0] = null;
Assert.IsNotNull (method.GetParameters () [0], "#2");
}
[Test]
public void Bug354757 ()
{
MethodInfo gmd = (typeof (MyList <int>)).GetMethod ("ConvertAll");
MethodInfo oi = gmd.MakeGenericMethod (gmd.GetGenericArguments ());
Assert.AreSame (gmd, oi);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
#if MOBILE
[Category ("NotWorking")] // #10552
#endif
public void MakeGenericMethodRespectConstraints ()
{
var m = typeof (MethodInfoTest).GetMethod ("TestMethod");
m.MakeGenericMethod (typeof (Type));
}
public void TestMethod <T> () where T : Exception
{
}
public class MyList<T>
{
public TOutput ConvertAll<TOutput> (Foo<T,TOutput> arg)
{
return default (TOutput);
}
public T ConvertAll2 (MyList<T> arg)
{
return default (T);
}
}
public class Foo<T,TOutput>
{
}
class GenericHelper<T>
{
public void Test (T t)
{
}
}
interface IMethodInvoke<out T>
{
T Test ();
}
class MethodInvoke : IMethodInvoke<string>
{
public string Test ()
{
return "MethodInvoke";
}
}
[Test]
public void GetInterfaceMapWorksWithVariantIfaces ()
{
var m0 = typeof (IMethodInvoke<object>).GetMethod ("Test");
var m1 = typeof (IMethodInvoke<string>).GetMethod ("Test");
var obj = new MethodInvoke ();
Assert.AreEqual ("MethodInvoke", m0.Invoke (obj, new Object [0]));
Assert.AreEqual ("MethodInvoke", m1.Invoke (obj, new Object [0]));
}
public int? Bug12856 ()
{
return null;
}
[Test] //Bug #12856
public void MethodToStringShouldPrintFullNameOfGenericStructs ()
{
var m = GetType ().GetMethod ("Bug12856");
Assert.AreEqual ("System.Nullable`1[System.Int32] Bug12856()", m.ToString (), "#1");
}
[Test]
public void GetReflectedType () // #12205
{
// public method declared in base type, queried from a derived type
MethodInfo mi = typeof (TestInheritedMethodB).GetMethod ("TestMethod2");
Assert.AreEqual (mi.ReflectedType, typeof (TestInheritedMethodB), "#1");
// public method declared in a generic class,
// queried from a non-generic class derived
// from an instantiation of the generic class.
mi = typeof (DerivedFromGenericBase).GetMethod ("f2");
Assert.AreEqual (mi.ReflectedType, typeof (DerivedFromGenericBase), "#2");
// public method declared in a generic class,
// queried from the generic type defintion of
// a generic derived class.
mi = typeof (GenericMid<,>).GetMethod ("f2");
Assert.AreEqual (mi.ReflectedType, typeof (GenericMid<,>), "#3");
// public method declared in a generic class,
// queried from an instantiation of a generic
// derived class.
mi = typeof (GenericMid<int,int>).GetMethod ("f2");
Assert.AreEqual (mi.ReflectedType, typeof (GenericMid<int,int>), "#4");
}
#if !MONOTOUCH && !FULL_AOT_RUNTIME
class GenericClass<T>
{
public void Method ()
{
T lv = default(T);
Console.WriteLine(lv);
}
public void Method2<K> (T a0, K a1)
{
T var0 = a0;
K var1 = a1;
Console.WriteLine (var0);
Console.WriteLine (var1);
}
}
static void EnsureMethodExists (Type type, string name, params Type[] parameterTypes)
{
var method = type.GetTypeInfo ().GetDeclaredMethods (name)
.SingleOrDefault (m => m.GetParameters ().Select (p => p.ParameterType).SequenceEqual (parameterTypes));
Assert.IsNotNull (method, $"{type}.{name}");
}
public void EnsureEntityFrameworkMethodsExist ()
{
// EntityFramework6 relies on the following methods
// see https://github.com/aspnet/EntityFramework6/blob/master/src/EntityFramework/Core/Objects/ELinq/MethodCallTranslator.cs#L846
// also https://github.com/mono/mono/pull/10452
EnsureMethodExists (typeof (Math), "Ceiling", typeof (decimal));
EnsureMethodExists (typeof (Math), "Ceiling", typeof (double));
EnsureMethodExists (typeof (Math), "Floor", typeof (decimal));
EnsureMethodExists (typeof (Math), "Floor", typeof (double));
EnsureMethodExists (typeof (Math), "Round", typeof (decimal));
EnsureMethodExists (typeof (Math), "Round", typeof (double));
EnsureMethodExists (typeof (Math), "Round", typeof (decimal), typeof (int));
EnsureMethodExists (typeof (Math), "Round", typeof (double), typeof (int));
EnsureMethodExists (typeof (Decimal), "Floor", typeof (decimal));
EnsureMethodExists (typeof (Decimal), "Ceiling", typeof (decimal));
EnsureMethodExists (typeof (Decimal), "Round", typeof (decimal));
EnsureMethodExists (typeof (Decimal), "Round", typeof (decimal), typeof (int));
EnsureMethodExists (typeof (String), "Replace", typeof (String), typeof (String));
EnsureMethodExists (typeof (String), "ToLower");
EnsureMethodExists (typeof (String), "ToUpper");
EnsureMethodExists (typeof (String), "Trim");
EnsureMethodExists (typeof (Math), "Truncate", typeof (decimal));
EnsureMethodExists (typeof (Math), "Truncate", typeof (double));
EnsureMethodExists (typeof (Math), "Pow", typeof (double), typeof (double));
EnsureMethodExists (typeof (Guid), "NewGuid");
EnsureMethodExists (typeof (String), "Contains", typeof (string));
EnsureMethodExists (typeof (String), "IndexOf", typeof (string));
EnsureMethodExists (typeof (String), "StartsWith", typeof (string));
EnsureMethodExists (typeof (String), "EndsWith", typeof (string));
EnsureMethodExists (typeof (String), "Substring", typeof (int));
EnsureMethodExists (typeof (String), "Substring", typeof (int), typeof (int));
EnsureMethodExists (typeof (String), "Remove", typeof (int));
EnsureMethodExists (typeof (String), "Remove", typeof (int), typeof (int));
EnsureMethodExists (typeof (String), "IsNullOrEmpty", typeof (string));
EnsureMethodExists (typeof (String), "Concat", typeof (string), typeof (string));
EnsureMethodExists (typeof (String), "Concat", typeof (string), typeof (string), typeof (string));
EnsureMethodExists (typeof (String), "Concat", typeof (string), typeof (string), typeof (string), typeof (string));
EnsureMethodExists (typeof (String), "Concat", typeof (object), typeof (object));
EnsureMethodExists (typeof (String), "Concat", typeof (object), typeof (object), typeof (object));
EnsureMethodExists (typeof (String), "Concat", typeof (object), typeof (object), typeof (object), typeof (object));
EnsureMethodExists (typeof (String), "Concat", typeof (object[]));
EnsureMethodExists (typeof (String), "Concat", typeof (string[]));
EnsureMethodExists (typeof (String), "Trim", typeof (Char[]));
EnsureMethodExists (typeof (String), "TrimStart", typeof (Char[]));
EnsureMethodExists (typeof (String), "TrimEnd", typeof (Char[]));
}
[Test]
public void TestLocalVariableTypes ()
{
var typeofT = typeof (GenericClass<>).GetGenericArguments () [0];
var typeofK = typeof (GenericClass<>).GetMethod ("Method2").GetGenericArguments () [0];
var type = typeof (GenericClass<>).GetMethod("Method").GetMethodBody().LocalVariables[0].LocalType;
Assert.AreEqual (typeofT, type);
Assert.AreEqual (typeof (GenericClass<>), type.DeclaringType);
bool foundTypeOfK = false;
bool foundExpectedType = false;
MethodBody mb = typeof (GenericClass<>).GetMethod("Method2").GetMethodBody();
foreach (LocalVariableInfo lvi in mb.LocalVariables) {
if (lvi.LocalType == typeofK) {
foundTypeOfK = true;
Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#1-1");
} else if (lvi.LocalType == typeofT) {
foundExpectedType = true;
Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#1-2");
}
}
Assert.IsTrue (foundTypeOfK, "#1-3");
if (mb.LocalVariables.Count < 2)
Assert.Ignore ("Code built in release mode - 'T var0' optmized out");
else
Assert.IsTrue (foundExpectedType, "#1-4");
foundTypeOfK = false;
foundExpectedType = false;
mb = typeof (GenericClass<int>).GetMethod("Method2").GetMethodBody();
foreach (LocalVariableInfo lvi in mb.LocalVariables) {
if (lvi.LocalType == typeofK) {
foundTypeOfK = true;
Assert.AreEqual (typeof (GenericClass<>), lvi.LocalType.DeclaringType, "#2-1");
} else if (lvi.LocalType == typeof (int)) {
foundExpectedType = true;
}
}
Assert.IsTrue (foundTypeOfK, "#2-3");
if (mb.LocalVariables.Count < 2)
Assert.Ignore ("Code built in release mode - 'int var0' optmized out");
else
Assert.IsTrue (foundExpectedType, "#2-4");
}
#endif
}
// Helper class
class RefOnlyMethodClass
{
// Helper static method
static void RefOnlyMethod ()
{
}
}
public class MethodHandleTest<T>
{
private List<T> _myList = new List<T> ();
public MethodHandleTest ()
{
_myList.Add (default (T));
}
public List<T> MyList {
get { return _myList; }
set { _myList = value; }
}
}
}