2 using System
.Reflection
;
10 [AttributeUsage(AttributeTargets
.All
)]
11 public class TestAttribute
: Attribute
18 public void Test (int a
)
24 public delegate Delegate
test_delegate(int i
);
51 public event test_delegate e_1
;
54 public event test_delegate e_2
;
59 // TODO: Where to apply ?
62 public event test_delegate e_1
{
67 public event test_delegate e_2
{
74 public event test_delegate e_3
{
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
)
92 if (!expected_presence
&& attrs
.Length
== 0)
95 Console
.WriteLine ("#" + tc
.ToString () + " failed");
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;