2 using System
.Reflection
;
3 using System
.Runtime
.CompilerServices
;
11 [AttributeUsage(AttributeTargets
.All
)]
12 public class TestAttribute
: Attribute
19 public void Test (int a
)
25 public delegate Delegate
test_delegate(int i
);
52 public event test_delegate e_1
;
55 public event test_delegate e_2
;
60 // TODO: Where to apply ?
63 public event test_delegate e_1
{
68 public event test_delegate e_2
{
75 public event test_delegate e_3
{
84 public class ClassMain
86 static bool failed
= false;
88 static void Assert (object[] attrs
, bool expected_presence
, int tc
)
90 if (attrs
.Length
== 1 && expected_presence
)
93 if (!expected_presence
&& attrs
.Length
== 0)
96 Console
.WriteLine ("#" + tc
.ToString () + " failed");
100 public static int Main () {
101 MethodInfo mi
= typeof (Test_1
).GetMethod ("Test");
102 Assert (mi
.GetParameters ()[0].GetCustomAttributes (true), false, 1);
103 Assert (mi
.GetCustomAttributes (true), false, 2);
104 Assert (mi
.ReturnTypeCustomAttributes
.GetCustomAttributes (true), true, 3);
106 mi
= typeof (test_delegate
).GetMethod ("Invoke");
107 Assert (mi
.GetParameters ()[0].GetCustomAttributes (true), false, 4);
108 Assert (mi
.GetCustomAttributes (true), false, 5);
109 Assert (mi
.ReturnTypeCustomAttributes
.GetCustomAttributes (true), true, 6);
111 /* Under net 2.0, SerializableAttribute is returned */
112 if (typeof (test_delegate
).GetCustomAttributes (false).Length
!= 1)
113 Assert (typeof (test_delegate
).GetCustomAttributes (false), false, 7);
115 PropertyInfo pi
= typeof (Test_2
).GetProperty ("Test");
116 Assert (pi
.GetCustomAttributes (true), false, 31);
117 Assert (pi
.GetGetMethod ().GetCustomAttributes (true), false, 32);
118 Assert (pi
.GetGetMethod ().ReturnTypeCustomAttributes
.GetCustomAttributes (true), true, 33);
119 Assert (pi
.GetSetMethod ().GetCustomAttributes (true), false, 34);
120 Assert (pi
.GetSetMethod ().ReturnTypeCustomAttributes
.GetCustomAttributes (true), true, 35);
121 pi
= typeof (Test_2
).GetProperty ("Test2");
122 Assert (pi
.GetCustomAttributes (true), false, 36);
123 Assert (pi
.GetSetMethod ().GetCustomAttributes (true), false, 37);
124 Assert (pi
.GetSetMethod ().ReturnTypeCustomAttributes
.GetCustomAttributes (true), false, 38);
125 Assert (pi
.GetSetMethod ().GetParameters ()[0].GetCustomAttributes (true), true, 39);
127 EventInfo ei
= typeof(Test_3
).GetEvent ("e_1");
128 Assert (ei
.GetCustomAttributes (true), false, 41);
129 Assert (ei
.GetAddMethod ().GetCustomAttributes (true), false, 42);
130 Assert (ei
.GetAddMethod ().ReturnTypeCustomAttributes
.GetCustomAttributes (true), false, 43);
131 Assert (ei
.GetRemoveMethod ().GetCustomAttributes (true), false, 44);
132 Assert (ei
.GetRemoveMethod ().ReturnTypeCustomAttributes
.GetCustomAttributes (true), false, 45);
133 FieldInfo fi
= typeof(Test_3
).GetField ("e_1", BindingFlags
.NonPublic
| BindingFlags
.Public
| BindingFlags
.Instance
);
134 Assert (fi
.GetCustomAttributes (typeof (CompilerGeneratedAttribute
), true), true, 46);
135 Assert (fi
.GetCustomAttributes (typeof (TestAttribute
), true), true, 47);
137 ei
= typeof(Test_3
).GetEvent ("e_2");
138 Assert (ei
.GetCustomAttributes (true), false, 51);
139 Assert (ei
.GetAddMethod ().GetCustomAttributes (true), true, 52);
140 Assert (ei
.GetAddMethod ().ReturnTypeCustomAttributes
.GetCustomAttributes (true), false, 53);
141 Assert (ei
.GetRemoveMethod ().GetCustomAttributes (true), true, 54);
142 Assert (ei
.GetRemoveMethod ().ReturnTypeCustomAttributes
.GetCustomAttributes (true), false, 55);
143 fi
= typeof(Test_3
).GetField ("e_2", BindingFlags
.NonPublic
| BindingFlags
.Public
| BindingFlags
.Instance
);
144 Assert (fi
.GetCustomAttributes (typeof (CompilerGeneratedAttribute
), true), true, 56);
145 Assert (fi
.GetCustomAttributes (typeof (TestAttribute
), true), false, 57);
147 ei
= typeof(Test_4
).GetEvent ("e_2");
148 Assert (ei
.GetCustomAttributes (true), false, 71);
149 Assert (ei
.GetAddMethod ().GetCustomAttributes (true), false, 72);
150 Assert (ei
.GetAddMethod ().ReturnTypeCustomAttributes
.GetCustomAttributes (true), true, 73);
151 Assert (ei
.GetRemoveMethod ().GetCustomAttributes (true), false, 74);
152 Assert (ei
.GetRemoveMethod ().ReturnTypeCustomAttributes
.GetCustomAttributes (true), true, 75);
153 fi
= typeof(Test_3
).GetField ("e_2", BindingFlags
.NonPublic
| BindingFlags
.Public
| BindingFlags
.Instance
);
154 Assert (fi
.GetCustomAttributes (typeof (CompilerGeneratedAttribute
), true), true, 76);
155 Assert (fi
.GetCustomAttributes (typeof (TestAttribute
), true), false, 77);
157 ei
= typeof(Test_4
).GetEvent ("e_3");
158 Assert (ei
.GetCustomAttributes (true), false, 81);
159 Assert (ei
.GetAddMethod ().GetCustomAttributes (true), false, 82);
160 Assert (ei
.GetAddMethod ().ReturnTypeCustomAttributes
.GetCustomAttributes (true), false, 83);
161 Assert (ei
.GetAddMethod ().GetParameters ()[0].GetCustomAttributes (true), true, 84);
162 Assert (ei
.GetRemoveMethod ().GetCustomAttributes (true), false, 85);
163 Assert (ei
.GetRemoveMethod ().ReturnTypeCustomAttributes
.GetCustomAttributes (true), false, 86);
164 Assert (ei
.GetRemoveMethod ().GetParameters ()[0].GetCustomAttributes (true), true, 87);
165 fi
= typeof(Test_3
).GetField ("e_2", BindingFlags
.NonPublic
| BindingFlags
.Public
| BindingFlags
.Instance
);
166 Assert (fi
.GetCustomAttributes (typeof (CompilerGeneratedAttribute
), true), true, 86);
167 Assert (fi
.GetCustomAttributes (typeof (TestAttribute
), true), false, 87);
169 return failed
? 1 : 0;