ParameterInfoTest.cs 5.2 KB

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