ParameterInfoTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // ParameterInfoTest - NUnit Test Cases for the ParameterInfo class
  3. //
  4. // Zoltan Varga ([email protected])
  5. //
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  8. //
  9. //
  10. using System;
  11. using System.Threading;
  12. using System.Reflection;
  13. using System.Runtime.InteropServices;
  14. using System.Runtime.CompilerServices;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Reflection
  17. {
  18. public class Marshal1 : ICustomMarshaler
  19. {
  20. public static ICustomMarshaler GetInstance (string s)
  21. {
  22. return new Marshal1 ();
  23. }
  24. public void CleanUpManagedData (object managedObj)
  25. {
  26. }
  27. public void CleanUpNativeData (IntPtr pNativeData)
  28. {
  29. }
  30. public int GetNativeDataSize ()
  31. {
  32. return 4;
  33. }
  34. public IntPtr MarshalManagedToNative (object managedObj)
  35. {
  36. return IntPtr.Zero;
  37. }
  38. public object MarshalNativeToManaged (IntPtr pNativeData)
  39. {
  40. return null;
  41. }
  42. }
  43. [TestFixture]
  44. public class ParameterInfoTest
  45. {
  46. [Test]
  47. public void IsDefined_AttributeType_Null ()
  48. {
  49. MethodInfo mi = typeof (object).GetMethod ("Equals",
  50. new Type [1] { typeof (object) });
  51. ParameterInfo pi = mi.GetParameters () [0];
  52. try {
  53. pi.IsDefined ((Type) null, false);
  54. Assert.Fail ("#1");
  55. } catch (ArgumentNullException ex) {
  56. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  57. Assert.IsNull (ex.InnerException, "#3");
  58. Assert.IsNotNull (ex.Message, "#4");
  59. Assert.IsNotNull (ex.ParamName, "#5");
  60. Assert.AreEqual ("attributeType", ex.ParamName, "#6");
  61. }
  62. }
  63. #if NET_2_0 && !NET_2_1
  64. public enum ParamEnum {
  65. None = 0,
  66. Foo = 1,
  67. Bar = 2
  68. };
  69. public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, ParamEnum n = ParamEnum.Foo)
  70. {
  71. }
  72. #if !TARGET_JVM // No support for extern methods in TARGET_JVM
  73. [DllImport ("foo")]
  74. public extern static void marshalAsMethod (
  75. [MarshalAs(UnmanagedType.Bool)]int p0,
  76. [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
  77. [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
  78. #endif
  79. [Test]
  80. public void DefaultValueEnum () {
  81. ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
  82. Assert.AreEqual (typeof (ParamEnum), info [5].DefaultValue.GetType (), "#1");
  83. Assert.AreEqual (ParamEnum.Foo, info [5].DefaultValue, "#2");
  84. }
  85. public static void Sample2 ([DecimalConstantAttribute(2,2,2,2,2)] decimal a, [DateTimeConstantAttribute(123456)] DateTime b) {}
  86. [Test]
  87. public void DefaultValuesFromCustomAttr () {
  88. ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("Sample2").GetParameters ();
  89. Assert.AreEqual (typeof (Decimal), info [0].DefaultValue.GetType (), "#1");
  90. Assert.AreEqual (typeof (DateTime), info [1].DefaultValue.GetType (), "#2");
  91. }
  92. [Test] // bug #339013
  93. public void TestDefaultValues ()
  94. {
  95. ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
  96. Assert.AreEqual (pi [0].DefaultValue.GetType (), typeof (DBNull), "#1");
  97. Assert.AreEqual (pi [1].DefaultValue.GetType (), typeof (Missing), "#2");
  98. }
  99. public void Sample (int a, [Optional] int b)
  100. {
  101. }
  102. [Test]
  103. public void PseudoCustomAttributes () {
  104. ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
  105. Assert.AreEqual (0, info[0].GetCustomAttributes (true).Length, "#A1");
  106. Assert.AreEqual (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length, "#A2");
  107. Assert.AreEqual (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length, "#A3");
  108. Assert.AreEqual (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length, "#A4");
  109. Assert.AreEqual (2, info[4].GetCustomAttributes (true).Length, "#A5");
  110. #if !TARGET_JVM // No support for extern methods in TARGET_JVM
  111. ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
  112. MarshalAsAttribute attr;
  113. attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
  114. Assert.AreEqual (UnmanagedType.Bool, attr.Value, "#B");
  115. attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
  116. Assert.AreEqual (UnmanagedType.LPArray, attr.Value, "#C1");
  117. Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType, "#C2");
  118. attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
  119. Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#D1");
  120. Assert.AreEqual ("5", attr.MarshalCookie, "#D2");
  121. Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#D3");
  122. #endif
  123. }
  124. [Test] // bug #342536
  125. public void Generics_Name ()
  126. {
  127. MethodInfo mi;
  128. Type type;
  129. ParameterInfo [] info;
  130. type = typeof (BaseType<string>);
  131. mi = type.GetMethod ("GetItems");
  132. Assert.IsNotNull (mi, "#A1");
  133. info = mi.GetParameters ();
  134. Assert.AreEqual (1, info.Length, "#A2");
  135. Assert.AreEqual ("count", info [0].Name, "#A3");
  136. mi = type.GetMethod ("Add");
  137. Assert.IsNotNull (mi, "#B1");
  138. info = mi.GetParameters ();
  139. Assert.AreEqual (2, info.Length, "#B2");
  140. Assert.AreEqual ("item", info [0].Name, "#B3");
  141. Assert.AreEqual ("index", info [1].Name, "#B4");
  142. mi = type.GetMethod ("Create");
  143. Assert.IsNotNull (mi, "#C1");
  144. info = mi.GetParameters ();
  145. Assert.AreEqual (2, info.Length, "#C2");
  146. Assert.AreEqual ("x", info [0].Name, "#C3");
  147. Assert.AreEqual ("item", info [1].Name, "#C4");
  148. }
  149. public class BaseType <T>
  150. {
  151. public void GetItems (int count)
  152. {
  153. }
  154. public void Add (T item, int index)
  155. {
  156. }
  157. public V Create <V> (int x, T item)
  158. {
  159. return default (V);
  160. }
  161. }
  162. #endif
  163. [Test]
  164. public void Member () {
  165. ParameterInfo parm = typeof (Derived).GetMethod ("SomeMethod").GetParameters()[0];
  166. Assert.AreEqual (typeof (Derived), parm.Member.ReflectedType);
  167. Assert.AreEqual (typeof (Base), parm.Member.DeclaringType);
  168. }
  169. [Test]
  170. public void ArrayMethodParameters ()
  171. {
  172. var matrix_int_get = typeof (int[,,]).GetMethod ("Get");
  173. var parameters = matrix_int_get.GetParameters ();
  174. Assert.AreEqual (3, parameters.Length);
  175. Assert.AreEqual (0, parameters [0].GetCustomAttributes (false).Length);
  176. Assert.AreEqual (0, parameters [1].GetCustomAttributes (false).Length);
  177. Assert.AreEqual (0, parameters [2].GetCustomAttributes (false).Length);
  178. }
  179. class Base
  180. {
  181. public void SomeMethod( int x )
  182. {
  183. }
  184. }
  185. class Derived : Base
  186. {
  187. }
  188. #if NET_4_0
  189. public static void TestC (decimal u = decimal.MaxValue) {
  190. }
  191. [Test]
  192. public void DefaultValueDecimal () {
  193. var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
  194. Assert.AreEqual (decimal.MaxValue, info [0].DefaultValue);
  195. }
  196. #endif
  197. }
  198. }