ParameterInfoTest.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. using System.Reflection.Emit;
  13. using System.Runtime.InteropServices;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Reflection
  16. {
  17. public class Marshal1 : ICustomMarshaler
  18. {
  19. public static ICustomMarshaler GetInstance (string s) {
  20. return new Marshal1 ();
  21. }
  22. public void CleanUpManagedData (object managedObj)
  23. {
  24. }
  25. public void CleanUpNativeData (IntPtr pNativeData)
  26. {
  27. }
  28. public int GetNativeDataSize ()
  29. {
  30. return 4;
  31. }
  32. public IntPtr MarshalManagedToNative (object managedObj)
  33. {
  34. return IntPtr.Zero;
  35. }
  36. public object MarshalNativeToManaged (IntPtr pNativeData)
  37. {
  38. return null;
  39. }
  40. }
  41. [TestFixture]
  42. public class ParameterInfoTest : Assertion
  43. {
  44. public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m) {
  45. }
  46. [DllImport ("foo")]
  47. public extern static void marshalAsMethod (
  48. [MarshalAs(UnmanagedType.Bool)]int p0,
  49. [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
  50. [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
  51. #if NET_2_0
  52. [Test]
  53. public void PseudoCustomAttributes () {
  54. ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
  55. AssertEquals (0, info[0].GetCustomAttributes (true).Length);
  56. AssertEquals (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length);
  57. AssertEquals (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length);
  58. AssertEquals (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length);
  59. AssertEquals (2, info[4].GetCustomAttributes (true).Length);
  60. ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
  61. MarshalAsAttribute attr;
  62. attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
  63. AssertEquals (UnmanagedType.Bool, attr.Value);
  64. attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
  65. AssertEquals (UnmanagedType.LPArray, attr.Value);
  66. AssertEquals (UnmanagedType.LPStr, attr.ArraySubType);
  67. attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
  68. AssertEquals (UnmanagedType.CustomMarshaler, attr.Value);
  69. AssertEquals ("5", attr.MarshalCookie);
  70. AssertEquals (typeof (Marshal1), Type.GetType (attr.MarshalType));
  71. }
  72. #endif
  73. }
  74. }