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.Private.CoreLib
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.Enumeration
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
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
legacy
resources
Makefile
System.IO.platformnotsupported.sources
corlib.dll.sources.REMOVED.git-id
corlib_test.dll.sources
corlib_xtest.dll.sources
linux_build_corlib.dll.exclude.sources
linux_build_corlib.dll.sources
linux_net_4_x_corlib.dll.exclude.sources
linux_net_4_x_corlib.dll.sources
macos_build_corlib.dll.exclude.sources
macos_build_corlib.dll.sources
macos_net_4_x_corlib.dll.exclude.sources
macos_net_4_x_corlib.dll.sources
mobile_tests.exclude.sources
monodroid_corlib.dll.exclude.sources
monodroid_corlib.dll.sources
monodroid_corlib_test.dll.exclude.sources
monodroid_corlib_xtest.dll.exclude.sources
monotouch_corlib.dll.exclude.sources
monotouch_corlib.dll.sources
monotouch_corlib_test.dll.exclude.sources
monotouch_corlib_xtest.dll.exclude.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_corlib_test.dll.exclude.sources
monotouch_tv_corlib_xtest.dll.exclude.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_corlib_test.dll.exclude.sources
monotouch_watch_corlib_xtest.dll.exclude.sources
monotouch_watch_runtime_corlib.dll.exclude.sources
monotouch_watch_runtime_corlib.dll.sources
net_4_x_corlib.dll.sources
orbis_corlib.dll.exclude.sources
orbis_corlib.dll.sources
orbis_corlib_test.dll.exclude.sources
testing_aot_common_corlib.dll.exclude.sources
testing_aot_common_corlib.dll.sources
testing_aot_common_corlib_test.dll.exclude.sources
testing_aot_full_corlib.dll.exclude.sources
testing_aot_full_corlib.dll.sources
testing_aot_full_corlib_test.dll.exclude.sources
testing_aot_full_interp_corlib.dll.exclude.sources
testing_aot_full_interp_corlib.dll.sources
testing_aot_full_interp_corlib_test.dll.exclude.sources
testing_aot_hybrid_corlib.dll.exclude.sources
testing_aot_hybrid_corlib.dll.sources
testing_aot_hybrid_corlib_test.dll.exclude.sources
unix_build_corlib.dll.exclude.sources
unix_build_corlib.dll.sources
unix_net_4_x_corlib.dll.exclude.sources
unix_net_4_x_corlib.dll.sources
unreal_corlib.dll.exclude.sources
unreal_corlib.dll.sources
wasm_corlib.dll.exclude.sources
wasm_corlib.dll.sources
wasm_corlib_test.dll.exclude.sources
wasm_corlib_xtest.dll.exclude.sources
win32_build_corlib.dll.exclude.sources
win32_build_corlib.dll.sources
win32_monodroid_corlib.dll.exclude.sources
win32_monodroid_corlib.dll.sources
win32_monodroid_corlib_test.dll.exclude.sources
win32_net_4_x_corlib.dll.exclude.sources
win32_net_4_x_corlib.dll.sources
win32_net_4_x_corlib_test.dll.exclude.sources
win32_net_4_x_corlib_test.dll.sources
win32_net_4_x_corlib_xtest.dll.sources
winaot_corlib.dll.exclude.sources
winaot_corlib.dll.sources
winaot_corlib_test.dll.exclude.sources
winaot_corlib_test.dll.sources
xammac_corlib.dll.exclude.sources
xammac_corlib.dll.sources
xammac_corlib_test.dll.exclude.sources
xammac_corlib_xtest.dll.exclude.sources
xammac_net_4_5_corlib.dll.exclude.sources
xammac_net_4_5_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
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/ParameterInfoTest.cs
Xamarin Public Jenkins (auto-signing) 64ac736ec5 Imported Upstream version 6.0.0.172
Former-commit-id: f3cc9b82f3e5bd8f0fd3ebc098f789556b44e9cd
2019-04-12 14:10:50 +00:00

577 lines
17 KiB
C#

//
// ParameterInfoTest - NUnit Test Cases for the ParameterInfo class
//
// Zoltan Varga (vargaz@freemail.hu)
//
// (C) Ximian, Inc. http://www.ximian.com
// Copyright 2011 Xamarin Inc (http://www.xamarin.com).
//
//
using System;
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;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.Reflection
{
public class Marshal1 : ICustomMarshaler
{
public static ICustomMarshaler GetInstance (string s)
{
return new Marshal1 ();
}
public void CleanUpManagedData (object managedObj)
{
}
public void CleanUpNativeData (IntPtr pNativeData)
{
}
public int GetNativeDataSize ()
{
return 4;
}
public IntPtr MarshalManagedToNative (object managedObj)
{
return IntPtr.Zero;
}
public object MarshalNativeToManaged (IntPtr pNativeData)
{
return null;
}
}
[TestFixture]
public class ParameterInfoTest
{
[Test]
public void IsDefined_AttributeType_Null ()
{
MethodInfo mi = typeof (object).GetMethod ("Equals",
new Type [1] { typeof (object) });
ParameterInfo pi = mi.GetParameters () [0];
try {
pi.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");
}
}
#if !MOBILE
public enum ParamEnum {
None = 0,
Foo = 1,
Bar = 2
};
public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, ParamEnum n = ParamEnum.Foo)
{
}
[DllImport ("foo")]
public extern static void marshalAsMethod (
[MarshalAs(UnmanagedType.Bool)]int p0,
[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
[MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
[Test]
public void DefaultValueEnum () {
ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
Assert.AreEqual (typeof (ParamEnum), info [5].DefaultValue.GetType (), "#1");
Assert.AreEqual (ParamEnum.Foo, info [5].DefaultValue, "#2");
}
[Test]
public void HasDefaultValueEnum () {
ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
Assert.IsTrue (info [5].HasDefaultValue);
}
public static void Sample2 ([DecimalConstantAttribute(2,2,2,2,2)] decimal a, [DateTimeConstantAttribute(123456)] DateTime b) {}
[Test]
public void DefaultValuesFromCustomAttr () {
ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("Sample2").GetParameters ();
Assert.AreEqual (typeof (Decimal), info [0].DefaultValue.GetType (), "#1");
Assert.AreEqual (typeof (DateTime), info [1].DefaultValue.GetType (), "#2");
}
[Test] // bug #339013
public void TestDefaultValues ()
{
ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
Assert.AreEqual (pi [0].DefaultValue.GetType (), typeof (DBNull), "#1");
Assert.AreEqual (pi [1].DefaultValue.GetType (), typeof (Missing), "#2");
}
[Test]
public void TestHasDefaultValues ()
{
ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
Assert.IsFalse (pi [0].HasDefaultValue, "#1");
Assert.IsFalse (pi [1].HasDefaultValue, "#2");
Assert.IsTrue (pi [2].HasDefaultValue, "#3");
}
public void Sample (int a, [Optional] int b, object c = null)
{
}
[Test]
public void PseudoCustomAttributes () {
ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
Assert.AreEqual (0, info[0].GetCustomAttributes (true).Length, "#A1");
Assert.AreEqual (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length, "#A2");
Assert.AreEqual (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length, "#A3");
Assert.AreEqual (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length, "#A4");
Assert.AreEqual (2, info[4].GetCustomAttributes (true).Length, "#A5");
ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
MarshalAsAttribute attr;
attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
Assert.AreEqual (UnmanagedType.Bool, attr.Value, "#B");
attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
Assert.AreEqual (UnmanagedType.LPArray, attr.Value, "#C1");
Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType, "#C2");
attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#D1");
Assert.AreEqual ("5", attr.MarshalCookie, "#D2");
Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#D3");
}
[Test] // bug #342536
public void Generics_Name ()
{
MethodInfo mi;
Type type;
ParameterInfo [] info;
type = typeof (BaseType<string>);
mi = type.GetMethod ("GetItems");
Assert.IsNotNull (mi, "#A1");
info = mi.GetParameters ();
Assert.AreEqual (1, info.Length, "#A2");
Assert.AreEqual ("count", info [0].Name, "#A3");
mi = type.GetMethod ("Add");
Assert.IsNotNull (mi, "#B1");
info = mi.GetParameters ();
Assert.AreEqual (2, info.Length, "#B2");
Assert.AreEqual ("item", info [0].Name, "#B3");
Assert.AreEqual ("index", info [1].Name, "#B4");
mi = type.GetMethod ("Create");
Assert.IsNotNull (mi, "#C1");
info = mi.GetParameters ();
Assert.AreEqual (2, info.Length, "#C2");
Assert.AreEqual ("x", info [0].Name, "#C3");
Assert.AreEqual ("item", info [1].Name, "#C4");
}
public class BaseType <T>
{
public void GetItems (int count)
{
}
public void Add (T item, int index)
{
}
public V Create <V> (int x, T item)
{
return default (V);
}
}
#endif
[Test]
public void Member () {
ParameterInfo parm = typeof (Derived).GetMethod ("SomeMethod").GetParameters()[0];
Assert.AreEqual (typeof (Derived), parm.Member.ReflectedType);
Assert.AreEqual (typeof (Base), parm.Member.DeclaringType);
}
[Test]
public void ArrayMethodParameters ()
{
var matrix_int_get = typeof (int[,,]).GetMethod ("Get");
var parameters = matrix_int_get.GetParameters ();
Assert.AreEqual (3, parameters.Length);
Assert.AreEqual (0, parameters [0].GetCustomAttributes (false).Length);
Assert.AreEqual (0, parameters [1].GetCustomAttributes (false).Length);
Assert.AreEqual (0, parameters [2].GetCustomAttributes (false).Length);
}
class Base
{
public void SomeMethod( int x )
{
}
}
class Derived : Base
{
}
public static void TestC (decimal u = decimal.MaxValue) {
}
[Test]
public void DefaultValueDecimal () {
var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
Assert.AreEqual (decimal.MaxValue, info [0].DefaultValue);
}
[Test]
public void HasDefaultValueDecimal () {
var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
Assert.IsTrue (info [0].HasDefaultValue);
}
class TestParamAttribute : Attribute
{
}
public static int TestCustomAttribute_Method ([TestParamAttribute] string arg)
{
return arg.Length;
}
[Test]
public void TestCustomAttribute ()
{
var metInfo = GetType ().GetMethod ("TestCustomAttribute_Method", new Type[] { typeof(string) });
var paramInfos = metInfo.GetParameters ();
var argParamInfo = paramInfos[0];
var custAttrs = argParamInfo.GetCustomAttributes ();
Assert.AreEqual (1, custAttrs.Count ());
}
class MyParameterInfo2 : ParameterInfo
{
public ParameterAttributes MyAttrsImpl;
public override ParameterAttributes Attributes {
get {return MyAttrsImpl;}
}
public IList<CustomAttributeData> myList = new List<CustomAttributeData> ();
public override IList<CustomAttributeData> GetCustomAttributesData () {
return myList;
}
}
class MyParameterInfo : ParameterInfo
{
public void SetClassImpl (Type t)
{
ClassImpl = t;
}
public void SetDefaultValueImpl (object o)
{
DefaultValueImpl = o;
}
public void SetMemberImpl (MemberInfo o)
{
MemberImpl = o;
}
public void SetNameImpl (string s)
{
NameImpl = s;
}
public void SetPositionImpl (int i)
{
PositionImpl = i;
}
public void SetAttrsImpl (ParameterAttributes a)
{
AttrsImpl = a;
}
public void TestMethod (int a) {}
public int this[int x, int y] {
get { return 0; }
set { }
}
}
[Test]
public void SubClassWithNoOverrides ()
{
var p = new MyParameterInfo ();
Assert.IsFalse (p.IsDefined (typeof (FlagsAttribute), false), "#1");
Assert.AreEqual (0, p.GetCustomAttributes (false).Length, "#2");
Assert.AreEqual (0, p.GetCustomAttributes (typeof (FlagsAttribute), false).Length, "#3");
Assert.AreEqual (0, p.GetOptionalCustomModifiers ().Length, "#4");
Assert.AreEqual (0, p.GetRequiredCustomModifiers ().Length, "#5");
try {
var ign = p.HasDefaultValue;
Assert.Fail ("#6");
} catch (NotImplementedException) {
}
Assert.IsFalse (p.IsIn, "#7");
#if FEATURE_USE_LCID
Assert.IsFalse (p.IsLcid, "#8");
#endif
Assert.IsFalse (p.IsOptional, "#9");
Assert.IsFalse (p.IsOut, "#10");
Assert.IsFalse (p.IsRetval, "#10");
try {
var ign = p.CustomAttributes;
Assert.Fail ("#11");
} catch (NotImplementedException) {
}
try {
p.GetCustomAttributesData ();
Assert.Fail ("#12");
} catch (NotImplementedException) {
}
Assert.AreEqual (0x8000000, p.MetadataToken, "#13");
Assert.AreEqual (0, p.Position, "#14");
try {
var ign = p.DefaultValue;
Assert.Fail ("#15");
} catch (NotImplementedException) {
}
try {
var ign = p.RawDefaultValue;
Assert.Fail ("#16");
} catch (NotImplementedException) {
}
Assert.IsNull (p.Member, "#17");
Assert.AreEqual (ParameterAttributes.None, p.Attributes, "#18");
Assert.IsNull (p.Name, "#19");
Assert.IsNull (p.ParameterType, "#20");
}
[Test]
public void SubClassWithValuesSet ()
{
var p = new MyParameterInfo ();
p.SetClassImpl (typeof (Decimal));
Assert.AreEqual (typeof (Decimal), p.ParameterType, "#1");
p.SetClassImpl (null);
p.SetDefaultValueImpl ("foo");
try {
var ign = p.DefaultValue;
Assert.Fail ("#2");
} catch (NotImplementedException) {
}
p.SetDefaultValueImpl (null);
var obj = typeof (object);
p.SetMemberImpl (obj);
Assert.AreEqual (obj, p.Member, "#3");
Assert.AreEqual (0x8000000, p.MetadataToken, "#4");
p.SetMemberImpl (null);
var method = typeof (MyParameterInfo).GetMethod ("TestMethod");
p.SetMemberImpl (method);
Assert.IsNotNull (method, "#5");
Assert.AreEqual (method, p.Member, "#6");
Assert.AreEqual (0x8000000, p.MetadataToken, "#7");
p.SetMemberImpl (null);
var property = typeof (MyParameterInfo).GetProperty ("Item");
p.SetMemberImpl (property);
Assert.IsNotNull (property, "#8");
Assert.AreEqual (property, p.Member, "#9");
Assert.AreEqual (0x8000000, p.MetadataToken, "#10");
p.SetMemberImpl (null);
p.SetNameImpl ("foo");
Assert.AreEqual ("foo", p.Name, "#11");
p.SetNameImpl (null);
p.SetPositionImpl (99);
Assert.AreEqual (p.Position, 99, "#12");
Assert.AreEqual (p.MetadataToken, 0x8000000, "#13");
p.SetPositionImpl (0);
Assert.IsFalse (p.IsIn, "#14");
p.SetAttrsImpl (ParameterAttributes.In);
Assert.IsTrue (p.IsIn, "#15");
}
[Test]
public void SubClassWithOverrides ()
{
var p2 = new MyParameterInfo2 ();
Assert.IsFalse (p2.IsIn, "#1");
p2.MyAttrsImpl = ParameterAttributes.In;
Assert.IsTrue (p2.IsIn, "#2");
Assert.AreEqual (p2.myList, p2.CustomAttributes, "#3");
}
[Test]
public void ParameterInfoToString ()
{
var method = typeof (TestNestedClass).GetTypeInfo ().GetMethod ("TestMethod");
var sb = new StringBuilder ();
foreach (var parameter in method.GetParameters ())
{
sb.Append (parameter + "\n");
}
string actual = sb.ToString ();
string expected = "Int32 a0\n" +
"System.String a1\n" +
"TestNestedClass a2\n" +
"System.Collections.Generic.List`1[System.Int32] a3\n" +
"System.Collections.Generic.List`1[System.Text.StringBuilder] a4\n" +
"System.Collections.Generic.List`1[MonoTests.System.Reflection.ParameterInfoTest+TestNestedClass] a5\n" +
"System.Text.StringBuilder a6\n" +
"System.Collections.Generic.Dictionary`2[System.Int32,System.String] a7\n" +
"Int32& a8\n" +
"Int32& a9\n" +
"TestNestedClass& a10\n" +
"System.Collections.Generic.List`1[System.Int32]& a11\n";
Assert.AreEqual (expected, actual, "#1");
}
public class TestNestedClass
{
public static void TestMethod (int a0, string a1,
TestNestedClass a2, List<int> a3,
List<StringBuilder> a4, List<TestNestedClass> a5,
StringBuilder a6, Dictionary<int, string> a7,
out int a8, ref int a9, out TestNestedClass a10, out List<int> a11)
{
a8 = 0;
a9 = 0;
a10 = null;
a11 = null;
}
}
[Test] // https://github.com/mono/mono/issues/8312
// The linker removes the SkipWhile methods
[Category ("MobileNotWorking")]
public void ParameterInfoToStringForQueryableSkipWhile ()
{
var sb = new StringBuilder ();
var methods = typeof (Queryable).GetTypeInfo ().GetMethods ().Where (m => m.Name == "SkipWhile");
foreach (var methodInfo in methods)
{
foreach (var parameter in methodInfo.GetParameters ())
{
sb.Append (parameter + "\n");
}
}
string actual = sb.ToString ();
string expected = "System.Linq.IQueryable`1[TSource] source\n" +
"System.Linq.Expressions.Expression`1[System.Func`2[TSource,System.Boolean]] predicate\n" +
"System.Linq.IQueryable`1[TSource] source\n" +
"System.Linq.Expressions.Expression`1[System.Func`3[TSource,System.Int32,System.Boolean]] predicate\n";
Assert.AreEqual (expected, actual, "#1");
}
public class Dummy {
public void M1 (decimal? arg = 12.345M) { }
public void M2 ([Optional, DecimalConstant (1, 2, 3, 4, 5)] decimal? arg) { }
public void M3 (decimal? arg = null) { }
public void M4 ([Optional, DateTimeConstant (1L)] DateTime? arg) { }
public void M5 (DateTime? arg = null) { }
}
[Test]
// https://github.com/mono/mono/issues/11303
public void RawDefaultValue_Nullable ()
{
var type = typeof (Dummy);
Assert.AreEqual (12.345M, type.GetMethod("M1").GetParameters () [0].RawDefaultValue);
Assert.AreEqual (new DecimalConstantAttribute (1, 2, 3, 4, 5).Value, type.GetMethod ("M2").GetParameters () [0].RawDefaultValue);
Assert.AreEqual (null, type.GetMethod ("M3").GetParameters () [0].RawDefaultValue);
Assert.AreEqual (new DateTime (1), type.GetMethod ("M4").GetParameters () [0].RawDefaultValue);
Assert.AreEqual (null, type.GetMethod ("M5").GetParameters () [0].RawDefaultValue);
}
public sealed class A { }
public sealed class B { }
public sealed class C { }
public sealed class D { }
#if !MONOTOUCH && !FULL_AOT_RUNTIME
[Test]
// https://github.com/mono/mono/issues/11302
public void CustomModifiersOrder()
{
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("SomeAssembly"), AssemblyBuilderAccess.RunAndSave);
var moduleBuilder = assemblyBuilder.DefineDynamicModule ("SomeAssembly", "SomeAssembly.dll");
var typeBuilder = moduleBuilder.DefineType ("SomeClass", TypeAttributes.Class);
var methodBuilder = typeBuilder.DefineMethod (
"SomeMethod",
MethodAttributes.Public,
CallingConventions.HasThis,
typeof (void),
null,
null,
new[] { typeof (int) },
new[] { new[] { typeof (A), typeof (B), typeof (A), typeof (A) } },
new[] { new[] { typeof (C), typeof (D), typeof (C), typeof (C) } });
methodBuilder.GetILGenerator ().Emit (OpCodes.Ret);
var type = typeBuilder.CreateType ();
var method = type.GetMethod ("SomeMethod");
var parameter = method.GetParameters () [0];
var requiredCustomModifiers = parameter.GetRequiredCustomModifiers ();
Assert.AreEqual (new[] { typeof (A), typeof (A), typeof (B), typeof (A) }, requiredCustomModifiers);
var optionalCustomModifiers = parameter.GetOptionalCustomModifiers ();
Assert.AreEqual (new[] { typeof (C), typeof (C), typeof (D), typeof (C) }, optionalCustomModifiers);
}
#endif
[Test]
public void ReturnParameter_IsDefined_False ()
{
Type type = typeof (object);
MethodInfo method = type.GetMethod ("ToString");
ParameterInfo paramInfo = method.ReturnParameter;
Assert.IsFalse (paramInfo.IsDefined (typeof (Attribute)));
}
}
}