ParameterInfoTest.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 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 : Assertion
  45. {
  46. #if NET_2_0
  47. public enum ParamEnum {
  48. None = 0,
  49. Foo = 1,
  50. Bar = 2
  51. };
  52. public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m, [DefaultParameterValue (ParamEnum.Foo)] ParamEnum n) {
  53. }
  54. #if !TARGET_JVM
  55. [DllImport ("foo")]
  56. public extern static void marshalAsMethod (
  57. [MarshalAs(UnmanagedType.Bool)]int p0,
  58. [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
  59. [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
  60. #endif
  61. [Test]
  62. public void DefaultValueEnum () {
  63. ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
  64. AssertEquals (typeof (ParamEnum), info [5].DefaultValue.GetType ());
  65. AssertEquals (ParamEnum.Foo, info [5].DefaultValue);
  66. }
  67. [Test]
  68. public void PseudoCustomAttributes () {
  69. ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
  70. AssertEquals (0, info[0].GetCustomAttributes (true).Length);
  71. AssertEquals (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length);
  72. AssertEquals (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length);
  73. AssertEquals (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length);
  74. AssertEquals (2, info[4].GetCustomAttributes (true).Length);
  75. ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
  76. MarshalAsAttribute attr;
  77. attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
  78. AssertEquals (UnmanagedType.Bool, attr.Value);
  79. attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
  80. AssertEquals (UnmanagedType.LPArray, attr.Value);
  81. AssertEquals (UnmanagedType.LPStr, attr.ArraySubType);
  82. attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
  83. AssertEquals (UnmanagedType.CustomMarshaler, attr.Value);
  84. AssertEquals ("5", attr.MarshalCookie);
  85. AssertEquals (typeof (Marshal1), Type.GetType (attr.MarshalType));
  86. }
  87. #endif
  88. }
  89. }