In tests/attributes:
[mono.git] / mcs / class / corlib / Test / System.Reflection / ParameterInfoTest.cs
1 //
2 // ParameterInfoTest - NUnit Test Cases for the ParameterInfo class
3 //
4 // Zoltan Varga (vargaz@freemail.hu)
5 //
6 // (C) Ximian, Inc.  http://www.ximian.com
7 //
8 //
9
10 using System;
11 using System.Threading;
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Runtime.InteropServices;
15
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Reflection
19 {
20
21
22 public class Marshal1 : ICustomMarshaler
23 {
24         public static ICustomMarshaler GetInstance (string s) {
25                 return new Marshal1 ();
26         }
27
28         public void CleanUpManagedData (object managedObj)
29         {
30         }
31
32         public void CleanUpNativeData (IntPtr pNativeData)
33         {
34         }
35
36         public int GetNativeDataSize ()
37         {
38                 return 4;
39         }
40
41         public IntPtr MarshalManagedToNative (object managedObj)
42         {
43                 return IntPtr.Zero;
44         }
45
46         public object MarshalNativeToManaged (IntPtr pNativeData)
47         {
48                 return null;
49         }
50 }
51
52 [TestFixture]
53 public class ParameterInfoTest : Assertion
54 {
55         public static void paramMethod (int i, [In] int j, [Out] int k, [Optional] int l, [In,Out] int m) {
56         }
57
58         [DllImport ("foo")]
59         public extern static void marshalAsMethod (
60                 [MarshalAs(UnmanagedType.Bool)]int p0, 
61                 [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string [] p1,
62                 [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object p2);
63
64 #if NET_2_0
65         [Test]
66         public void PseudoCustomAttributes () {
67                 ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
68                 AssertEquals (0, info[0].GetCustomAttributes (true).Length);
69                 AssertEquals (1, info[1].GetCustomAttributes (typeof (InAttribute), true).Length);
70                 AssertEquals (1, info[2].GetCustomAttributes (typeof (OutAttribute), true).Length);
71                 AssertEquals (1, info[3].GetCustomAttributes (typeof (OptionalAttribute), true).Length);
72                 AssertEquals (2, info[4].GetCustomAttributes (true).Length);
73
74                 ParameterInfo[] pi = typeof (ParameterInfoTest).GetMethod ("marshalAsMethod").GetParameters ();
75                 MarshalAsAttribute attr;
76
77                 attr = (MarshalAsAttribute)(pi [0].GetCustomAttributes (true) [0]);
78                 AssertEquals (UnmanagedType.Bool, attr.Value);
79
80                 attr = (MarshalAsAttribute)(pi [1].GetCustomAttributes (true) [0]);
81                 AssertEquals (UnmanagedType.LPArray, attr.Value);
82                 AssertEquals (UnmanagedType.LPStr, attr.ArraySubType);
83
84                 attr = (MarshalAsAttribute)(pi [2].GetCustomAttributes (true) [0]);
85                 AssertEquals (UnmanagedType.CustomMarshaler, attr.Value);
86                 AssertEquals ("5", attr.MarshalCookie);
87                 AssertEquals (typeof (Marshal1), Type.GetType (attr.MarshalType));
88         }
89 #endif
90 }               
91 }