2 // ParameterInfoTest - NUnit Test Cases for the ParameterInfo class
4 // Zoltan Varga (vargaz@freemail.hu)
6 // (C) Ximian, Inc. http://www.ximian.com
7 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
12 using System.Threading;
13 using System.Reflection;
14 using System.Runtime.InteropServices;
15 using System.Runtime.CompilerServices;
16 using System.Collections.Generic;
18 using NUnit.Framework;
20 namespace MonoTests.System.Reflection
22 public class Marshal1 : ICustomMarshaler
24 public static ICustomMarshaler GetInstance (string s)
26 return new Marshal1 ();
29 public void CleanUpManagedData (object managedObj)
33 public void CleanUpNativeData (IntPtr pNativeData)
37 public int GetNativeDataSize ()
42 public IntPtr MarshalManagedToNative (object managedObj)
47 public object MarshalNativeToManaged (IntPtr pNativeData)
54 public class ParameterInfoTest
57 public void IsDefined_AttributeType_Null ()
59 MethodInfo mi = typeof (object).GetMethod ("Equals",
60 new Type [1] { typeof (object) });
61 ParameterInfo pi = mi.GetParameters () [0];
64 pi.IsDefined ((Type) null, false);
66 } catch (ArgumentNullException ex) {
67 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
68 Assert.IsNull (ex.InnerException, "#3");
69 Assert.IsNotNull (ex.Message, "#4");
70 Assert.IsNotNull (ex.ParamName, "#5");
71 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
75 #if NET_2_0 && !NET_2_1
76 public enum ParamEnum {
82 public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, ParamEnum n = ParamEnum.Foo)
86 #if !TARGET_JVM // No support for extern methods in TARGET_JVM
88 public extern static void marshalAsMethod (
89 [MarshalAs(UnmanagedType.Bool)]int p0,
90 [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
91 [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
94 public void DefaultValueEnum () {
95 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
97 Assert.AreEqual (typeof (ParamEnum), info [5].DefaultValue.GetType (), "#1");
98 Assert.AreEqual (ParamEnum.Foo, info [5].DefaultValue, "#2");
101 public static void Sample2 ([DecimalConstantAttribute(2,2,2,2,2)] decimal a, [DateTimeConstantAttribute(123456)] DateTime b) {}
104 public void DefaultValuesFromCustomAttr () {
105 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("Sample2").GetParameters ();
107 Assert.AreEqual (typeof (Decimal), info [0].DefaultValue.GetType (), "#1");
108 Assert.AreEqual (typeof (DateTime), info [1].DefaultValue.GetType (), "#2");
111 [Test] // bug #339013
112 public void TestDefaultValues ()
114 ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
116 Assert.AreEqual (pi [0].DefaultValue.GetType (), typeof (DBNull), "#1");
117 Assert.AreEqual (pi [1].DefaultValue.GetType (), typeof (Missing), "#2");
120 public void Sample (int a, [Optional] int b)
125 public void PseudoCustomAttributes () {
126 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
127 Assert.AreEqual (0, info[0].GetCustomAttributes (true).Length, "#A1");
128 Assert.AreEqual (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length, "#A2");
129 Assert.AreEqual (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length, "#A3");
130 Assert.AreEqual (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length, "#A4");
131 Assert.AreEqual (2, info[4].GetCustomAttributes (true).Length, "#A5");
133 #if !TARGET_JVM // No support for extern methods in TARGET_JVM
134 ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
135 MarshalAsAttribute attr;
137 attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
138 Assert.AreEqual (UnmanagedType.Bool, attr.Value, "#B");
140 attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
141 Assert.AreEqual (UnmanagedType.LPArray, attr.Value, "#C1");
142 Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType, "#C2");
144 attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
145 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#D1");
146 Assert.AreEqual ("5", attr.MarshalCookie, "#D2");
147 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#D3");
151 [Test] // bug #342536
152 public void Generics_Name ()
156 ParameterInfo [] info;
158 type = typeof (BaseType<string>);
160 mi = type.GetMethod ("GetItems");
161 Assert.IsNotNull (mi, "#A1");
162 info = mi.GetParameters ();
163 Assert.AreEqual (1, info.Length, "#A2");
164 Assert.AreEqual ("count", info [0].Name, "#A3");
166 mi = type.GetMethod ("Add");
167 Assert.IsNotNull (mi, "#B1");
168 info = mi.GetParameters ();
169 Assert.AreEqual (2, info.Length, "#B2");
170 Assert.AreEqual ("item", info [0].Name, "#B3");
171 Assert.AreEqual ("index", info [1].Name, "#B4");
173 mi = type.GetMethod ("Create");
174 Assert.IsNotNull (mi, "#C1");
175 info = mi.GetParameters ();
176 Assert.AreEqual (2, info.Length, "#C2");
177 Assert.AreEqual ("x", info [0].Name, "#C3");
178 Assert.AreEqual ("item", info [1].Name, "#C4");
181 public class BaseType <T>
183 public void GetItems (int count)
187 public void Add (T item, int index)
191 public V Create <V> (int x, T item)
199 public void Member () {
200 ParameterInfo parm = typeof (Derived).GetMethod ("SomeMethod").GetParameters()[0];
201 Assert.AreEqual (typeof (Derived), parm.Member.ReflectedType);
202 Assert.AreEqual (typeof (Base), parm.Member.DeclaringType);
206 public void ArrayMethodParameters ()
208 var matrix_int_get = typeof (int[,,]).GetMethod ("Get");
209 var parameters = matrix_int_get.GetParameters ();
211 Assert.AreEqual (3, parameters.Length);
212 Assert.AreEqual (0, parameters [0].GetCustomAttributes (false).Length);
213 Assert.AreEqual (0, parameters [1].GetCustomAttributes (false).Length);
214 Assert.AreEqual (0, parameters [2].GetCustomAttributes (false).Length);
219 public void SomeMethod( int x )
229 public static void TestC (decimal u = decimal.MaxValue) {
233 public void DefaultValueDecimal () {
234 var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
235 Assert.AreEqual (decimal.MaxValue, info [0].DefaultValue);
238 class MyParameterInfo2 : ParameterInfo
240 public ParameterAttributes MyAttrsImpl;
242 public override ParameterAttributes Attributes {
243 get {return MyAttrsImpl;}
246 public IList<CustomAttributeData> myList = new List<CustomAttributeData> ();
248 public override IList<CustomAttributeData> GetCustomAttributesData () {
253 class MyParameterInfo : ParameterInfo
255 public void SetClassImpl (Type t)
260 public void SetDefaultValueImpl (object o)
262 DefaultValueImpl = o;
265 public void SetMemberImpl (MemberInfo o)
270 public void SetNameImpl (string s)
275 public void SetPositionImpl (int i)
280 public void SetAttrsImpl (ParameterAttributes a)
285 public void TestMethod (int a) {}
286 public int this[int x, int y] {
294 public void SubClassWithNoOverrides ()
296 var p = new MyParameterInfo ();
297 Assert.IsFalse (p.IsDefined (typeof (FlagsAttribute), false), "#1");
298 Assert.AreEqual (0, p.GetCustomAttributes (false).Length, "#2");
299 Assert.AreEqual (0, p.GetCustomAttributes (typeof (FlagsAttribute), false).Length, "#3");
300 Assert.AreEqual (0, p.GetOptionalCustomModifiers ().Length, "#4");
301 Assert.AreEqual (0, p.GetRequiredCustomModifiers ().Length, "#5");
304 var ign = p.HasDefaultValue;
306 } catch (NotImplementedException) {
309 Assert.IsFalse (p.IsIn, "#7");
310 Assert.IsFalse (p.IsLcid, "#8");
311 Assert.IsFalse (p.IsOptional, "#9");
312 Assert.IsFalse (p.IsOut, "#10");
313 Assert.IsFalse (p.IsRetval, "#10");
316 var ign = p.CustomAttributes;
318 } catch (NotImplementedException) {
322 p.GetCustomAttributesData ();
324 } catch (NotImplementedException) {
327 Assert.AreEqual (0x8000000, p.MetadataToken, "#13");
328 Assert.AreEqual (0, p.Position, "#14");
330 var ign = p.DefaultValue;
332 } catch (NotImplementedException) {
335 var ign = p.RawDefaultValue;
337 } catch (NotImplementedException) {
339 Assert.IsNull (p.Member, "#17");
340 Assert.AreEqual (ParameterAttributes.None, p.Attributes, "#18");
341 Assert.IsNull (p.Name, "#19");
342 Assert.IsNull (p.ParameterType, "#20");
346 public void SubClassWithValuesSet ()
348 var p = new MyParameterInfo ();
349 p.SetClassImpl (typeof (Decimal));
350 Assert.AreEqual (typeof (Decimal), p.ParameterType, "#1");
351 p.SetClassImpl (null);
353 p.SetDefaultValueImpl ("foo");
355 var ign = p.DefaultValue;
357 } catch (NotImplementedException) {
359 p.SetDefaultValueImpl (null);
361 var obj = typeof (object);
362 p.SetMemberImpl (obj);
363 Assert.AreEqual (obj, p.Member, "#3");
364 Assert.AreEqual (0x8000000, p.MetadataToken, "#4");
365 p.SetMemberImpl (null);
367 var method = typeof (MyParameterInfo).GetMethod ("TestMethod");
368 p.SetMemberImpl (method);
369 Assert.IsNotNull (method, "#5");
370 Assert.AreEqual (method, p.Member, "#6");
371 Assert.AreEqual (0x8000000, p.MetadataToken, "#7");
372 p.SetMemberImpl (null);
374 var property = typeof (MyParameterInfo).GetProperty ("Item");
375 p.SetMemberImpl (property);
376 Assert.IsNotNull (property, "#8");
377 Assert.AreEqual (property, p.Member, "#9");
378 Assert.AreEqual (0x8000000, p.MetadataToken, "#10");
379 p.SetMemberImpl (null);
381 p.SetNameImpl ("foo");
382 Assert.AreEqual ("foo", p.Name, "#11");
383 p.SetNameImpl (null);
385 p.SetPositionImpl (99);
386 Assert.AreEqual (p.Position, 99, "#12");
387 Assert.AreEqual (p.MetadataToken, 0x8000000, "#13");
388 p.SetPositionImpl (0);
390 Assert.IsFalse (p.IsIn, "#14");
391 p.SetAttrsImpl (ParameterAttributes.In);
392 Assert.IsTrue (p.IsIn, "#15");
396 public void SubClassWithOverrides()
398 var p2 = new MyParameterInfo2 ();
399 Assert.IsFalse (p2.IsIn, "#1");
400 p2.MyAttrsImpl = ParameterAttributes.In;
401 Assert.IsTrue (p2.IsIn, "#2");
403 Assert.AreEqual (p2.myList, p2.CustomAttributes, "#3");