2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / tests / test-254.cs
blob1971d2f4e4812fa82feb8fbed1c666537ce0a723
1 using System;
2 using System.Reflection;
4 [assembly: Test]
6 namespace N
10 [AttributeUsage(AttributeTargets.All)]
11 public class TestAttribute: Attribute
15 public class Test_1
17 [return: Test]
18 public void Test (int a)
23 [return: Test]
24 public delegate Delegate test_delegate(int i);
27 public class Test_2
29 public int Test
31 [return: Test]
32 get {
33 return 4;
36 [return: Test]
37 set {
41 public bool Test2
43 [param: Test]
44 set {}
48 public class Test_3
50 [field: Test]
51 public event test_delegate e_1;
53 [method: Test]
54 public event test_delegate e_2;
57 public class Test_4
59 // TODO: Where to apply ?
61 [event: Test]
62 public event test_delegate e_1 {
63 add {}
64 remove {}
67 public event test_delegate e_2 {
68 [return: Test]
69 add {}
70 [return: Test]
71 remove {}
74 public event test_delegate e_3 {
75 [param: Test]
76 add {}
77 [param: Test]
78 remove {}
83 public class ClassMain
85 static bool failed = false;
87 static void Assert (object[] attrs, bool expected_presence, int tc)
89 if (attrs.Length == 1 && expected_presence)
90 return;
92 if (!expected_presence && attrs.Length == 0)
93 return;
95 Console.WriteLine ("#" + tc.ToString () + " failed");
96 failed = true;
99 public static int Main () {
100 MethodInfo mi = typeof (Test_1).GetMethod ("Test");
101 Assert (mi.GetParameters ()[0].GetCustomAttributes (true), false, 1);
102 Assert (mi.GetCustomAttributes (true), false, 2);
103 Assert (mi.ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 3);
105 mi = typeof (test_delegate).GetMethod ("Invoke");
106 Assert (mi.GetParameters ()[0].GetCustomAttributes (true), false, 4);
107 Assert (mi.GetCustomAttributes (true), false, 5);
108 Assert (mi.ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 6);
110 /* Under net 2.0, SerializableAttribute is returned */
111 if (typeof (test_delegate).GetCustomAttributes (false).Length != 1)
112 Assert (typeof (test_delegate).GetCustomAttributes (false), false, 7);
114 PropertyInfo pi = typeof (Test_2).GetProperty ("Test");
115 Assert (pi.GetCustomAttributes (true), false, 31);
116 Assert (pi.GetGetMethod ().GetCustomAttributes (true), false, 32);
117 Assert (pi.GetGetMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 33);
118 Assert (pi.GetSetMethod ().GetCustomAttributes (true), false, 34);
119 Assert (pi.GetSetMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 35);
120 pi = typeof (Test_2).GetProperty ("Test2");
121 Assert (pi.GetCustomAttributes (true), false, 36);
122 Assert (pi.GetSetMethod ().GetCustomAttributes (true), false, 37);
123 Assert (pi.GetSetMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 38);
124 Assert (pi.GetSetMethod ().GetParameters ()[0].GetCustomAttributes (true), true, 39);
126 EventInfo ei = typeof(Test_3).GetEvent ("e_1");
127 Assert (ei.GetCustomAttributes (true), false, 41);
128 Assert (ei.GetAddMethod ().GetCustomAttributes (true), false, 42);
129 Assert (ei.GetAddMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 43);
130 Assert (ei.GetRemoveMethod ().GetCustomAttributes (true), false, 44);
131 Assert (ei.GetRemoveMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 45);
132 FieldInfo fi = typeof(Test_3).GetField ("e_1", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
133 Assert (fi.GetCustomAttributes (true), true, 46);
135 ei = typeof(Test_3).GetEvent ("e_2");
136 Assert (ei.GetCustomAttributes (true), false, 51);
137 Assert (ei.GetAddMethod ().GetCustomAttributes (true), true, 52);
138 Assert (ei.GetAddMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 53);
139 Assert (ei.GetRemoveMethod ().GetCustomAttributes (true), true, 54);
140 Assert (ei.GetRemoveMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 55);
141 fi = typeof(Test_3).GetField ("e_2", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
142 Assert (fi.GetCustomAttributes (true), false, 56);
144 ei = typeof(Test_4).GetEvent ("e_2");
145 Assert (ei.GetCustomAttributes (true), false, 71);
146 Assert (ei.GetAddMethod ().GetCustomAttributes (true), false, 72);
147 Assert (ei.GetAddMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 73);
148 Assert (ei.GetRemoveMethod ().GetCustomAttributes (true), false, 74);
149 Assert (ei.GetRemoveMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 75);
150 fi = typeof(Test_3).GetField ("e_2", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
151 Assert (fi.GetCustomAttributes (true), false, 76);
153 ei = typeof(Test_4).GetEvent ("e_3");
154 Assert (ei.GetCustomAttributes (true), false, 81);
155 Assert (ei.GetAddMethod ().GetCustomAttributes (true), false, 82);
156 Assert (ei.GetAddMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 83);
157 Assert (ei.GetAddMethod ().GetParameters ()[0].GetCustomAttributes (true), true, 84);
158 Assert (ei.GetRemoveMethod ().GetCustomAttributes (true), false, 85);
159 Assert (ei.GetRemoveMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 86);
160 Assert (ei.GetRemoveMethod ().GetParameters ()[0].GetCustomAttributes (true), true, 87);
161 fi = typeof(Test_3).GetField ("e_2", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
162 Assert (fi.GetCustomAttributes (true), false, 88);
164 return failed ? 1 : 0;