ParameterInfoTest.cs 6.3 KB

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