dd66b8c7a2c2868e3b8b9441626c7e7c397f0f85
[mono.git] / mcs / class / corlib / Test / System / AttributeTest.cs
1 //
2 // AttributeTest.cs - NUnit Test Cases for the System.Attribute class
3 //
4 // Authors:
5 //      Duco Fijma (duco@lorentz.xs4all.nl)
6 //      Gonzalo Paniagua (gonzalo@ximian.com)
7 //      Gert Driesen (drieseng@users.sourceforge.net)
8 //
9 //      (C) 2002 Duco Fijma
10 //      (c) 2004 Novell, Inc. (http://www.novell.com)
11 //
12
13 using System;
14 using System.Reflection;
15 #if !MONOTOUCH && !MOBILE_STATIC
16 using System.Reflection.Emit;
17 #endif
18 using System.Runtime.InteropServices;
19 using System.Threading;
20
21 using NUnit.Framework;
22
23 namespace MonoTests.System
24 {
25         using MonoTests.System.AttributeTestInternals;
26
27         namespace AttributeTestInternals
28         {
29                 [AttributeUsage (AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
30                 internal class MyCustomAttribute : Attribute
31                 {
32                         private string _info;
33
34                         public MyCustomAttribute (string info)
35                         {
36                                 _info = info;
37                         }
38
39                         public string Info {
40                                 get {
41                                         return _info;
42                                 }
43                         }
44                 }
45
46                 [AttributeUsage (AttributeTargets.Class)]
47                 internal class YourCustomAttribute : Attribute
48                 {
49                         private int _value;
50
51                         public YourCustomAttribute (int value)
52                         {
53                                 _value = value;
54                         }
55
56                         public int Value {
57                                 get {
58                                         return _value;
59                                 }
60                         }
61                 }
62
63                 [AttributeUsage (AttributeTargets.Class)]
64                 internal class UnusedAttribute : Attribute
65                 {
66                 }
67
68                 [MyCustomAttribute ("MyBaseClass"), YourCustomAttribute (37)]
69                 internal class MyClass
70                 {
71                         int Value {
72                                 get { return 42; }
73                         }
74
75                         public static void ParamsMethod(params object[] args)
76                         {
77                         }
78                 }
79
80                 [MyCustomAttribute ("MyDerivedClass")]
81                 internal class MyDerivedClass : MyClass
82                 {
83                         public void Do ()
84                         {
85                         }
86                 }
87
88                 class MyDerivedClassNoAttribute : MyClass
89                 {
90                 }
91
92                 internal class AttributeWithTypeId : Attribute
93                 {
94                         public override object TypeId {
95                                 get { return this; }
96                         }
97                 }
98         }
99
100         [TestFixture]
101         public class AttributeTest
102         {
103                 [Test]
104                 public void TestIsDefined ()
105                 {
106                         Assert.IsTrue (Attribute.IsDefined (typeof(MyDerivedClass), typeof(MyCustomAttribute)), "#1");
107                         Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute)), "#2");
108                         Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute)), "#3");
109                         Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (MyCustomAttribute), true), "#4");
110                         Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), true), "#5");
111                         Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#6");
112                         Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (MyCustomAttribute), false), "#7");
113                         Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), false), "#8");
114                         Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#9");
115                         Assert.IsTrue (Attribute.IsDefined (typeof (MyClass).GetMethod ("ParamsMethod").GetParameters () [0], typeof (ParamArrayAttribute), false), "#10");
116                         Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClassNoAttribute), typeof (MyCustomAttribute)), "#11");
117                 }
118
119                 [Test]
120                 public void IsDefined_PropertyInfo ()
121                 {
122                         PropertyInfo pi = typeof (TestBase).GetProperty ("PropBase3");
123                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#A1");
124                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#A2");
125                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#A3");
126                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#A4");
127                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#A5");
128                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#A6");
129
130                         pi = typeof (TestBase).GetProperty ("PropBase2");
131                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#C1");
132                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#C2");
133                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#C3");
134                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#C4");
135                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#C5");
136                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#C6");
137
138                         pi = typeof (TestSub).GetProperty ("PropBase2");
139                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#D1");
140                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#D2");
141                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#D3");
142                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#D4");
143                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#D5");
144                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#D6");
145                 }
146
147                 [Test]
148                 public void IsDefined_PropertyInfo_Override ()
149                 {
150                         PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
151                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
152                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#B2");
153                         Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
154                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#B4");
155                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#B5");
156                         Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#B6");
157                 }
158
159                 [Test]
160                 public void TestGetCustomAttribute ()
161                 {
162                         int i = 1;
163                         Type t = typeof(MyDerivedClass);
164                         try {
165                                 Assert.AreEqual  ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute), false))).Info, "#1");
166                                 i++;
167                                 Assert.IsNull (((YourCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(YourCustomAttribute), false))), "#2");
168                                 i++;
169                                 Assert.AreEqual ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute)))).Info, "#3");
170                                 i++;
171                                 Assert.IsNotNull (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)), "#4");
172                                 i++;
173                                 Assert.AreEqual (37, ((YourCustomAttribute) (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)))).Value, "#5");
174                         } catch (Exception e) {
175                                 Assert.Fail ("Unexpected exception thrown at i=" + i + " with t=" + t + ". e=" + e);
176                         }
177                 }
178
179                 [Test]
180                 public void GetCustomAttribute_PropertyInfo ()
181                 {
182                         PropertyInfo pi = typeof (TestBase).GetProperty ("PropBase3");
183                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
184                                 typeof (PropTestAttribute)), "#A1");
185                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
186                                 typeof (PropTestAttribute), false), "#A2");
187                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
188                                 typeof (PropTestAttribute), true), "#A3");
189                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
190                                 typeof (ComVisibleAttribute)), "#A4");
191                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
192                                 typeof (ComVisibleAttribute), false), "#A5");
193                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
194                                 typeof (ComVisibleAttribute), true), "#A6");
195
196                         pi = typeof (TestBase).GetProperty ("PropBase2");
197                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
198                                 typeof (PropTestAttribute)), "#C1");
199                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
200                                 typeof (PropTestAttribute), false), "#C2");
201                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
202                                 typeof (PropTestAttribute), true), "#C3");
203                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
204                                 typeof (ComVisibleAttribute)), "#C4");
205                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
206                                 typeof (ComVisibleAttribute), false), "#C5");
207                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
208                                 typeof (ComVisibleAttribute), true), "#C6");
209
210                         pi = typeof (TestSub).GetProperty ("PropBase2");
211                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
212                                 typeof (PropTestAttribute)), "#D1");
213                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
214                                 typeof (PropTestAttribute), false), "#D2");
215                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
216                                 typeof (PropTestAttribute), true), "#D3");
217                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
218                                 typeof (ComVisibleAttribute)), "#D4");
219                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
220                                 typeof (ComVisibleAttribute), false), "#D5");
221                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
222                                 typeof (ComVisibleAttribute), true), "#D6");
223                 }
224
225                 [Test]
226                 public void GetCustomAttribute_PropertyInfo_Override ()
227                 {
228                         PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
229                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
230                                 typeof (PropTestAttribute)), "#B1");
231                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
232                                 typeof (PropTestAttribute), false), "#B2");
233                         Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
234                                 typeof (PropTestAttribute), true), "#B3");
235                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
236                                 typeof (ComVisibleAttribute)), "#B4");
237                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
238                                 typeof (ComVisibleAttribute), false), "#B5");
239                         Assert.IsNull (Attribute.GetCustomAttribute (pi,
240                                 typeof (ComVisibleAttribute), true), "#B6");
241                 }
242
243                 /* Test for bug 54518 */
244                 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
245                 public class PropTestAttribute : Attribute
246                 {
247                         public PropTestAttribute ()
248                         {
249                         }
250                 }
251
252                 public class TestBase
253                 {
254                         [PropTest]
255                         public int PropBase1 {
256                                 get { return 0; }
257                                 set { }
258                         }
259
260                         [PropTest]
261                         [ComVisible (false)]
262                         public string PropBase2 {
263                                 get { return ""; }
264                                 set { }
265                         }
266
267                         [PropTest]
268                         public virtual string PropBase3 {
269                                 get { return ""; }
270                                 set { }
271                         }
272                 }
273
274                 public class TestSub : TestBase
275                 {
276                         [PropTest]
277                         public int PropSub1 {
278                                 get { return 0; }
279                                 set { }
280                         }
281
282                         [PropTest]
283                         public string PropSub2 {
284                                 get { return ""; }
285                                 set { }
286                         }
287
288                         public override string PropBase3 {
289                                 get { return ""; }
290                                 set { }
291                         }
292                 }
293
294                 [Test]
295                 public void GetCustomAttributes_Element_Null ()
296                 {
297                         // 
298                         // Assembly
299                         // 
300
301                         try {
302                                 Attribute.GetCustomAttributes ((Assembly) null);
303                                 Assert.Fail ("#A1");
304                         } catch (ArgumentNullException ex) {
305                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
306                                 Assert.IsNull (ex.InnerException, "#A3");
307                                 Assert.IsNotNull (ex.Message, "#A4");
308                                 Assert.IsNotNull (ex.ParamName, "#A5");
309                                 Assert.AreEqual ("element", ex.ParamName, "#A6");
310                         }
311
312                         try {
313                                 Attribute.GetCustomAttributes ((Assembly) null, false);
314                                 Assert.Fail ("#B1");
315                         } catch (ArgumentNullException ex) {
316                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
317                                 Assert.IsNull (ex.InnerException, "#B3");
318                                 Assert.IsNotNull (ex.Message, "#B4");
319                                 Assert.IsNotNull (ex.ParamName, "#B5");
320                                 Assert.AreEqual ("element", ex.ParamName, "#B6");
321                         }
322
323                         try {
324                                 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute));
325                                 Assert.Fail ("#C1");
326                         } catch (ArgumentNullException ex) {
327                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
328                                 Assert.IsNull (ex.InnerException, "#C3");
329                                 Assert.IsNotNull (ex.Message, "#C4");
330                                 Assert.IsNotNull (ex.ParamName, "#C5");
331                                 Assert.AreEqual ("element", ex.ParamName, "#C6");
332                         }
333
334                         try {
335                                 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute), false);
336                                 Assert.Fail ("#D1");
337                         } catch (ArgumentNullException ex) {
338                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
339                                 Assert.IsNull (ex.InnerException, "#D3");
340                                 Assert.IsNotNull (ex.Message, "#D4");
341                                 Assert.IsNotNull (ex.ParamName, "#D5");
342                                 Assert.AreEqual ("element", ex.ParamName, "#D6");
343                         }
344
345                         // 
346                         // MemberInfo
347                         // 
348
349                         try {
350                                 Attribute.GetCustomAttributes ((MemberInfo) null);
351                                 Assert.Fail ("#A1");
352                         } catch (ArgumentNullException ex) {
353                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
354                                 Assert.IsNull (ex.InnerException, "#A3");
355                                 Assert.IsNotNull (ex.Message, "#A4");
356                                 Assert.IsNotNull (ex.ParamName, "#A5");
357                                 Assert.AreEqual ("element", ex.ParamName, "#A6");
358                         }
359
360                         try {
361                                 Attribute.GetCustomAttributes ((MemberInfo) null, false);
362                                 Assert.Fail ("#B1");
363                         } catch (ArgumentNullException ex) {
364                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
365                                 Assert.IsNull (ex.InnerException, "#B3");
366                                 Assert.IsNotNull (ex.Message, "#B4");
367                                 Assert.IsNotNull (ex.ParamName, "#B5");
368                                 Assert.AreEqual ("element", ex.ParamName, "#B6");
369                         }
370
371                         try {
372                                 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute));
373                                 Assert.Fail ("#C1");
374                         } catch (ArgumentNullException ex) {
375                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
376                                 Assert.IsNull (ex.InnerException, "#C3");
377                                 Assert.IsNotNull (ex.Message, "#C4");
378                                 Assert.IsNotNull (ex.ParamName, "#C5");
379                                 Assert.AreEqual ("element", ex.ParamName, "#C6");
380                         }
381
382                         try {
383                                 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute), false);
384                                 Assert.Fail ("#D1");
385                         } catch (ArgumentNullException ex) {
386                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
387                                 Assert.IsNull (ex.InnerException, "#D3");
388                                 Assert.IsNotNull (ex.Message, "#D4");
389                                 Assert.IsNotNull (ex.ParamName, "#D5");
390                                 Assert.AreEqual ("element", ex.ParamName, "#D6");
391                         }
392
393                         // 
394                         // Module
395                         // 
396
397                         try {
398                                 Attribute.GetCustomAttributes ((Module) null);
399                                 Assert.Fail ("#A1");
400                         } catch (ArgumentNullException ex) {
401                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
402                                 Assert.IsNull (ex.InnerException, "#A3");
403                                 Assert.IsNotNull (ex.Message, "#A4");
404                                 Assert.IsNotNull (ex.ParamName, "#A5");
405                                 Assert.AreEqual ("element", ex.ParamName, "#A6");
406                         }
407
408                         try {
409                                 Attribute.GetCustomAttributes ((Module) null, false);
410                                 Assert.Fail ("#B1");
411                         } catch (ArgumentNullException ex) {
412                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
413                                 Assert.IsNull (ex.InnerException, "#B3");
414                                 Assert.IsNotNull (ex.Message, "#B4");
415                                 Assert.IsNotNull (ex.ParamName, "#B5");
416                                 Assert.AreEqual ("element", ex.ParamName, "#B6");
417                         }
418
419                         try {
420                                 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute));
421                                 Assert.Fail ("#C1");
422                         } catch (ArgumentNullException ex) {
423                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
424                                 Assert.IsNull (ex.InnerException, "#C3");
425                                 Assert.IsNotNull (ex.Message, "#C4");
426                                 Assert.IsNotNull (ex.ParamName, "#C5");
427                                 Assert.AreEqual ("element", ex.ParamName, "#C6");
428                         }
429
430                         try {
431                                 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute), false);
432                                 Assert.Fail ("#D1");
433                         } catch (ArgumentNullException ex) {
434                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
435                                 Assert.IsNull (ex.InnerException, "#D3");
436                                 Assert.IsNotNull (ex.Message, "#D4");
437                                 Assert.IsNotNull (ex.ParamName, "#D5");
438                                 Assert.AreEqual ("element", ex.ParamName, "#D6");
439                         }
440
441                         // 
442                         // ParameterInfo
443                         // 
444
445                         try {
446                                 Attribute.GetCustomAttributes ((ParameterInfo) null);
447                                 Assert.Fail ("#A1");
448                         } catch (ArgumentNullException ex) {
449                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
450                                 Assert.IsNull (ex.InnerException, "#A3");
451                                 Assert.IsNotNull (ex.Message, "#A4");
452                                 Assert.IsNotNull (ex.ParamName, "#A5");
453                                 Assert.AreEqual ("element", ex.ParamName, "#A6");
454                         }
455
456                         try {
457                                 Attribute.GetCustomAttributes ((ParameterInfo) null, false);
458                                 Assert.Fail ("#B1");
459                         } catch (ArgumentNullException ex) {
460                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
461                                 Assert.IsNull (ex.InnerException, "#B3");
462                                 Assert.IsNotNull (ex.Message, "#B4");
463                                 Assert.IsNotNull (ex.ParamName, "#B5");
464                                 Assert.AreEqual ("element", ex.ParamName, "#B6");
465                         }
466
467                         try {
468                                 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute));
469                                 Assert.Fail ("#C1");
470                         } catch (ArgumentNullException ex) {
471                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
472                                 Assert.IsNull (ex.InnerException, "#C3");
473                                 Assert.IsNotNull (ex.Message, "#C4");
474                                 Assert.IsNotNull (ex.ParamName, "#C5");
475                                 Assert.AreEqual ("element", ex.ParamName, "#C6");
476                         }
477
478                         try {
479                                 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute), false);
480                                 Assert.Fail ("#D1");
481                         } catch (ArgumentNullException ex) {
482                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
483                                 Assert.IsNull (ex.InnerException, "#D3");
484                                 Assert.IsNotNull (ex.Message, "#D4");
485                                 Assert.IsNotNull (ex.ParamName, "#D5");
486                                 Assert.AreEqual ("element", ex.ParamName, "#D6");
487                         }
488                 }
489
490                 [Test]
491                 public void GetCustomAttributes_PropertyInfo ()
492                 {
493                         object[] attrs;
494                         PropertyInfo pi;
495
496                         pi = typeof (TestBase).GetProperty ("PropBase3");
497                         attrs = Attribute.GetCustomAttributes (pi);
498                         Assert.AreEqual (1, attrs.Length, "#A1");
499                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A2");
500                         attrs = Attribute.GetCustomAttributes (pi, false);
501                         Assert.AreEqual (1, attrs.Length, "#A3");
502                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A4");
503                         attrs = Attribute.GetCustomAttributes (pi, true);
504                         Assert.AreEqual (1, attrs.Length, "#A5");
505                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A6");
506                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
507                         Assert.AreEqual (1, attrs.Length, "#A7");
508                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A8");
509                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
510                         Assert.AreEqual (1, attrs.Length, "#A9");
511                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A10");
512                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
513                         Assert.AreEqual (1, attrs.Length, "#A11");
514                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A12");
515                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
516                         Assert.AreEqual (0, attrs.Length, "#A13");
517                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
518                         Assert.AreEqual (0, attrs.Length, "#A14");
519                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
520                         Assert.AreEqual (0, attrs.Length, "#A15");
521
522                         pi = typeof (TestBase).GetProperty ("PropBase2");
523                         attrs = Attribute.GetCustomAttributes (pi);
524                         Assert.AreEqual (2, attrs.Length, "#C1");
525                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C2");
526                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C3");
527                         attrs = Attribute.GetCustomAttributes (pi, false);
528                         Assert.AreEqual (2, attrs.Length, "#C4");
529                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C5");
530                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C6");
531                         attrs = Attribute.GetCustomAttributes (pi, true);
532                         Assert.AreEqual (2, attrs.Length, "#C7");
533                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C8");
534                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C9");
535                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
536                         Assert.AreEqual (1, attrs.Length, "#C10");
537                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C11");
538                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
539                         Assert.AreEqual (1, attrs.Length, "#C12");
540                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C13");
541                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
542                         Assert.AreEqual (1, attrs.Length, "#C14");
543                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C15");
544                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
545                         Assert.AreEqual (1, attrs.Length, "#C16");
546                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C17");
547                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
548                         Assert.AreEqual (1, attrs.Length, "#C18");
549                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C19");
550                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
551                         Assert.AreEqual (1, attrs.Length, "#C20");
552                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C21");
553
554                         pi = typeof (TestSub).GetProperty ("PropBase2");
555                         attrs = Attribute.GetCustomAttributes (pi);
556                         Assert.AreEqual (2, attrs.Length, "#D1");
557                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D2");
558                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D3");
559                         attrs = Attribute.GetCustomAttributes (pi, false);
560                         Assert.AreEqual (2, attrs.Length, "#D4");
561                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D5");
562                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D6");
563                         attrs = Attribute.GetCustomAttributes (pi, true);
564                         Assert.AreEqual (2, attrs.Length, "#D7");
565                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D8");
566                         Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D9");
567                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
568                         Assert.AreEqual (1, attrs.Length, "#D10");
569                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D11");
570                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
571                         Assert.AreEqual (1, attrs.Length, "#D12");
572                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D13");
573                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
574                         Assert.AreEqual (1, attrs.Length, "#D14");
575                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D15");
576                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
577                         Assert.AreEqual (1, attrs.Length, "#D16");
578                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D17");
579                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
580                         Assert.AreEqual (1, attrs.Length, "#D18");
581                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D19");
582                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
583                         Assert.AreEqual (1, attrs.Length, "#D20");
584                         Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D21");
585
586                         pi = typeof (TestSub).GetProperty ("PropSub1");
587                         attrs = Attribute.GetCustomAttributes (pi);
588                         Assert.AreEqual (1, attrs.Length, "#E1");
589                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E2");
590                         attrs = Attribute.GetCustomAttributes (pi, false);
591                         Assert.AreEqual (1, attrs.Length, "#E3");
592                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E4");
593                         attrs = Attribute.GetCustomAttributes (pi, true);
594                         Assert.AreEqual (1, attrs.Length, "#E5");
595                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E6");
596                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
597                         Assert.AreEqual (1, attrs.Length, "#E7");
598                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E8");
599                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
600                         Assert.AreEqual (1, attrs.Length, "#E9");
601                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E10");
602                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
603                         Assert.AreEqual (1, attrs.Length, "#E11");
604                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E12");
605                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
606                         Assert.AreEqual (0, attrs.Length, "#E13");
607                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
608                         Assert.AreEqual (0, attrs.Length, "#E14");
609                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
610                         Assert.AreEqual (0, attrs.Length, "#D15");
611                 }
612
613                 [Test]
614                 public void GetCustomAttributes_PropertyInfo_Override ()
615                 {
616                         object [] attrs;
617                         PropertyInfo pi;
618
619                         pi = typeof (TestSub).GetProperty ("PropBase3");
620                         attrs = Attribute.GetCustomAttributes (pi);
621                         Assert.AreEqual (1, attrs.Length, "#B1");
622                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B2");
623                         attrs = Attribute.GetCustomAttributes (pi, false);
624                         Assert.AreEqual (0, attrs.Length, "#B3");
625                         attrs = Attribute.GetCustomAttributes (pi, true);
626                         Assert.AreEqual (1, attrs.Length, "#B4");
627                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B5");
628                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
629                         Assert.AreEqual (1, attrs.Length, "#B6");
630                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B7");
631                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
632                         Assert.AreEqual (0, attrs.Length, "#B8");
633                         attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
634                         Assert.AreEqual (1, attrs.Length, "#B9");
635                         Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B10");
636                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
637                         Assert.AreEqual (0, attrs.Length, "#B11");
638                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
639                         Assert.AreEqual (0, attrs.Length, "#B12");
640                         attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
641                         Assert.AreEqual (0, attrs.Length, "#B13");
642                 }
643
644                 [Test]
645                 public void GetCustomAttributeOK ()
646                 {
647                         Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
648                                 typeof(DerivedTestCustomAttributeInherit));
649                         Assert.IsNotNull (attribute);
650                 }
651
652                 [Test]
653                 [ExpectedException (typeof(AmbiguousMatchException))]
654                 public void GetCustomAttributeAmbiguous ()
655                 {
656                         Attribute.GetCustomAttribute (typeof(ClassA), typeof(TestCustomAttribute));
657                 }
658
659                 [Test]
660                 public void GetCustomAttributeNull ()
661                 {
662                         Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
663                                 typeof(DerivedTestCustomAttributeMultipleInherit));
664                         Assert.IsNull (attribute);
665                 }
666
667                 [Test]
668                 public void GetCustomAttributesTypeNoInherit ()
669                 {
670                         object[] attributes;
671
672                         attributes = Attribute.GetCustomAttributes (typeof(ClassA), false);
673                         Assert.AreEqual (3, attributes.Length, "#A1");
674                         Assert.AreEqual (1, GetAttributeCount (attributes,
675                                 typeof(TestCustomAttribute)), "#A2");
676                         Assert.AreEqual (1, GetAttributeCount (attributes,
677                                 typeof(DerivedTestCustomAttributeMultiple)), "#A3");
678                         Assert.AreEqual (1, GetAttributeCount (attributes,
679                                 typeof(DerivedTestCustomAttributeInherit)), "#A4");
680
681                         attributes = Attribute.GetCustomAttributes (typeof(ClassB), false);
682                         Assert.AreEqual (4, attributes.Length, "#B1");
683                         Assert.AreEqual (1, GetAttributeCount (attributes,
684                                 typeof(TestCustomAttribute)), "#B2");
685                         Assert.AreEqual (2, GetAttributeCount (attributes,
686                                 typeof(DerivedTestCustomAttributeMultiple)), "#B3");
687                         Assert.AreEqual (1, GetAttributeCount (attributes,
688                                 typeof(DerivedTestCustomAttributeMultipleInherit)), "#B4");
689                 }
690
691                 [Test]
692                 public void GetCustomAttributesTypeInherit ()
693                 {
694                         object[] attributes;
695
696                         attributes = Attribute.GetCustomAttributes (typeof(ClassA), true);
697                         Assert.AreEqual (3, attributes.Length, "#A1");
698                         Assert.AreEqual (1, GetAttributeCount (attributes,
699                                 typeof(TestCustomAttribute)), "#A2");
700                         Assert.AreEqual (1, GetAttributeCount (attributes,
701                                 typeof(DerivedTestCustomAttributeMultiple)), "#A3");
702                         Assert.AreEqual (1, GetAttributeCount (attributes,
703                                 typeof(DerivedTestCustomAttributeInherit)), "#A4");
704
705                         attributes = Attribute.GetCustomAttributes (typeof(ClassB), true);
706                         Assert.AreEqual (5, attributes.Length, "#B1");
707                         Assert.AreEqual (1, GetAttributeCount (attributes,
708                                 typeof(TestCustomAttribute)), "#B2");
709                         Assert.AreEqual (2, GetAttributeCount (attributes,
710                                 typeof(DerivedTestCustomAttributeMultiple)), "#B3");
711                         Assert.AreEqual (1, GetAttributeCount (attributes,
712                                 typeof(DerivedTestCustomAttributeInherit)), "#B4");
713                         Assert.AreEqual (1, GetAttributeCount (attributes,
714                                 typeof(DerivedTestCustomAttributeMultipleInherit)), "#B5");
715                 }
716
717                 [Test]
718                 public void TestEquality ()
719                 {
720                         MyCustomAttribute a = new MyCustomAttribute ("one");
721                         MyCustomAttribute b = new MyCustomAttribute ("two");
722                         MyCustomAttribute c = new MyCustomAttribute ("one");
723                         MyCustomAttribute d = a;
724                         
725                         Assert.IsTrue (a.Equals (c), "#1");
726                         Assert.IsTrue (c.Equals (a), "#2");
727                         Assert.IsFalse (c.Equals (b), "#3");
728                         Assert.IsFalse (b.Equals (a), "#4");
729                         Assert.IsFalse (b.Equals (c), "#5");
730                         Assert.IsTrue (a.Equals (a), "#6");
731                         Assert.IsTrue (a.Equals (d), "#7");
732                         Assert.IsFalse (a.Equals (null), "#8");
733                 }
734
735                 class UserType : TypeDelegator {
736                         public int GetCattr1;
737                         public int GetCattr2;
738                         public int IsDef;
739                         public bool lastInherit;
740                         public Type lastAttrType;
741
742                         public UserType (Type type) : base (type) {}
743                         
744                         public override object [] GetCustomAttributes (bool inherit)
745                         {
746                                 ++GetCattr1;
747                                 lastInherit = inherit;
748                                 lastAttrType = null;
749                                 return base.GetCustomAttributes (inherit);
750                         }
751
752                         public override object [] GetCustomAttributes (Type attributeType, bool inherit)
753                         {
754                                 ++GetCattr2;
755                                 lastInherit = inherit;
756                                 lastAttrType = attributeType;
757                                 return base.GetCustomAttributes (attributeType, inherit);
758                         }
759
760                         public override bool IsDefined (Type attributeType, bool inherit)
761                         {
762                                 ++IsDef;
763                                 lastInherit = inherit;
764                                 lastAttrType = attributeType;
765                                 return base.IsDefined (attributeType, inherit);
766                         }
767                 }
768
769                 [Test]
770                 public void GetCustomAttributeOnUserType ()
771                 {
772                         UserType type = new UserType (typeof (AttributeTest));
773                         var res = Attribute.GetCustomAttribute (type, typeof (TestFixtureAttribute));
774                         Assert.IsNotNull (res, "#1");
775                         Assert.AreEqual (typeof (TestFixtureAttribute), res.GetType (), "#2");
776
777                         Assert.AreEqual (0, type.IsDef, "#4");
778                         Assert.AreEqual (0, type.GetCattr1, "#5");
779                         Assert.AreEqual (1, type.GetCattr2, "#6");
780                         Assert.IsTrue (type.lastInherit, "#7");
781                         Assert.AreEqual (typeof (TestFixtureAttribute), type.lastAttrType, "#8");
782                 }
783
784                 [Test]
785                 public void GetCustomAttributeOnMethodInfo ()
786                 {
787                         MemberInfo method = typeof (AttributeTest).GetMethod ("GetCustomAttributeOnMethodInfo");
788                         var res = Attribute.GetCustomAttribute (method, typeof (TestAttribute));
789
790                         Assert.IsNotNull (res, "#1");
791                         Assert.AreEqual (typeof (TestAttribute), res.GetType (), "#2");
792                 }
793
794                 [Test]
795                 public void GetCustomAttributesOnUserType ()
796                 {
797                         UserType type = new UserType (typeof (AttributeTest));
798                         var res = Attribute.GetCustomAttributes (type);
799                         Assert.IsNotNull (res, "#1");
800                         Assert.AreEqual (1, res.Length, "#2");
801                         Assert.AreEqual (typeof (TestFixtureAttribute), res [0].GetType (), "#3");
802
803                         Assert.AreEqual (0, type.IsDef, "#4");
804                         Assert.AreEqual (0, type.GetCattr1, "#5");
805                         Assert.AreEqual (1, type.GetCattr2, "#6");
806                         Assert.IsTrue (type.lastInherit, "#7");
807                         Assert.AreEqual (typeof (Attribute), type.lastAttrType, "#8");
808                 }
809
810                 [Test]
811                 public void IsDefinedOnUserType ()
812                 {
813                         UserType type = new UserType (typeof (AttributeTest));
814                         var res = Attribute.IsDefined (type, typeof (TestFixtureAttribute));
815                         Assert.IsTrue (res, "#1");
816
817                         Assert.AreEqual (1, type.IsDef, "#4");
818                         Assert.AreEqual (0, type.GetCattr1, "#5");
819                         Assert.AreEqual (0, type.GetCattr2, "#6");
820                         Assert.IsTrue (type.lastInherit, "#7");
821                         Assert.AreEqual (typeof (TestFixtureAttribute), type.lastAttrType, "#8");
822                 }
823
824                 [Test]
825                 public void IsDefinedForPseudoAttribute ()
826                 {                       
827                         Assert.IsTrue (Attribute.IsDefined (typeof (object), typeof(SerializableAttribute), true), "#1");
828                         Assert.IsFalse (Attribute.IsDefined (typeof (AttributeTest), typeof(SerializableAttribute), true), "#2");
829                 }
830
831                 [YourCustomAttribute (0)]
832                 [Serializable]
833                 [MyCustomAttribute ("")]
834                 class ClassForOrderIsImportant
835                 {
836                 }
837
838                 [Test]
839                 // The linker removes the serializable attribute
840                 [Category ("MobileNotWorking")]
841                 public void OrderIsImportant ()
842                 {
843                         var custom = typeof (ClassForOrderIsImportant).GetCustomAttributes (false);
844                         Assert.IsTrue (custom [0].GetType () == typeof (YourCustomAttribute));
845                         Assert.IsTrue (custom [1].GetType () == typeof (MyCustomAttribute));
846                         Assert.IsTrue (custom [2].GetType () == typeof (SerializableAttribute));
847                 }
848
849 #if !MONOTOUCH && !MOBILE_STATIC
850                 [Test]
851                 public void GetCustomAttributeOnNewSreTypes ()
852                 {
853                         AssemblyName assemblyName = new AssemblyName ();
854                         assemblyName.Name = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
855                         AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
856                                         assemblyName, AssemblyBuilderAccess.Run);
857                         ModuleBuilder module = assembly.DefineDynamicModule ("module1");
858
859                         var tb = module.DefineType ("ns.type", TypeAttributes.Public);
860                         var arr = tb.MakeArrayType ();
861                         var ptr = tb.MakePointerType ();
862                         var byref = tb.MakeByRefType ();
863
864                         try {
865                                 Attribute.GetCustomAttribute (arr, typeof (ObsoleteAttribute));
866                                 Assert.Fail ("#1");
867                         } catch (NotSupportedException) {}
868
869                         try {
870                                 Attribute.GetCustomAttribute (ptr, typeof (ObsoleteAttribute));
871                                 Assert.Fail ("#2");
872                         } catch (NotSupportedException) {}
873
874                         try {
875                                 Attribute.GetCustomAttribute (byref, typeof (ObsoleteAttribute));
876                                 Assert.Fail ("#3");
877                         } catch (NotSupportedException) {}
878                 }
879
880                 [Test]
881                 public void GetCustomAttributeOnBadSreTypes ()
882                 {
883                         AssemblyName assemblyName = new AssemblyName ();
884                         assemblyName.Name = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
885                         AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
886                                         assemblyName, AssemblyBuilderAccess.Run);
887                         ModuleBuilder module = assembly.DefineDynamicModule ("module1");
888
889                         var tb = module.DefineType ("ns.type", TypeAttributes.Public);
890                         tb.DefineGenericParameters ("T");
891                         var ginst = tb.MakeGenericType (typeof (int));
892                         try {
893                                 Attribute.GetCustomAttribute (ginst, typeof (ObsoleteAttribute));
894                                 Assert.Fail ("#1");
895                         } catch (NotSupportedException) {}
896                 }
897 #endif
898                 [Test] //Regression test for #499569
899                 public void GetCattrOnPropertyAndInheritance ()
900                 {
901                         var m = typeof(Sub).GetProperty ("Name");
902                         var res = Attribute.GetCustomAttributes (m, typeof(MyAttribute), true);
903                         Assert.AreEqual (1, res.Length, "#1");
904                 }
905
906                 abstract class Root
907                 {
908                         [MyAttribute]
909                         public abstract void Foo ();
910                 }
911
912                 abstract class Abs : Root
913                 {
914                         public abstract string Name { get; set; }
915                 }
916                 
917                 class Base: Abs
918                 {
919                         [MyAttribute]
920                         public override string Name {
921                                 get { return ""; }
922                                 set {}
923                         }
924
925                         public override void Foo () { }
926                 }
927                 
928                 class Sub: Base
929                 {
930                         public override string Name {
931                                 get { return ""; }
932                                 set {}
933                         }
934                 }
935                 
936                 class MySubAttribute: MyAttribute
937                 {
938                 }
939                 
940                 class MyAttribute: Attribute
941                 {
942                 }
943
944                 private int GetAttributeCount (object[] attributes, Type attributeType)
945                 {
946                         int counter = 0;
947
948                         foreach (Attribute attribute in attributes) {
949                                 if (attribute.GetType () == attributeType)
950                                         counter++;
951                         }
952
953                         return counter;
954                 }
955
956                 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
957                 private class TestCustomAttribute : Attribute
958                 {
959                 }
960
961                 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = false)]
962                 private class DerivedTestCustomAttributeMultiple : TestCustomAttribute
963                 {
964                 }
965
966                 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
967                 private class DerivedTestCustomAttributeInherit : TestCustomAttribute
968                 {
969                 }
970
971                 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = true)]
972                 private class DerivedTestCustomAttributeMultipleInherit : TestCustomAttribute
973                 {
974                 }
975
976                 [TestCustomAttribute]
977                 [DerivedTestCustomAttributeMultiple]
978                 [DerivedTestCustomAttributeInherit]
979                 private class ClassA
980                 {
981                 }
982
983                 [TestCustomAttribute ()]
984                 [DerivedTestCustomAttributeMultiple ()]
985                 [DerivedTestCustomAttributeMultiple ()]
986                 [DerivedTestCustomAttributeMultipleInherit ()]
987                 private class ClassB : ClassA
988                 {
989                 }
990
991                 [TestCustomAttribute ()]
992                 [DerivedTestCustomAttributeMultiple ()]
993                 [DerivedTestCustomAttributeMultipleInherit ()]
994                 private class ClassC : ClassB
995                 {
996                 }
997
998                 [Test]
999                 public void EmptyNonOverridenGetHashCode ()
1000                 {
1001                         MyAttribute a1 = new MyAttribute ();
1002                         MyAttribute a2 = new MyAttribute ();
1003                         Assert.AreEqual (a1.GetHashCode (), a2.GetHashCode (), "identical argument-less");
1004                         Assert.AreEqual (a1.GetHashCode (), a1.TypeId.GetHashCode (), "Empty/TypeId");
1005
1006                         MySubAttribute b1 = new MySubAttribute ();
1007                         Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
1008                         Assert.AreEqual (b1.GetHashCode (), b1.TypeId.GetHashCode (), "Empty/TypeId/Sub");
1009                 }
1010
1011                 class MyOwnCustomAttribute : MyCustomAttribute {
1012
1013                         public MyOwnCustomAttribute (string s)
1014                                 : base (s)
1015                         {
1016                         }
1017                 }
1018
1019                 [Test]
1020                 public void NonEmptyNonOverridenGetHashCode ()
1021                 {
1022                         MyCustomAttribute a1 = new MyCustomAttribute (null);
1023                         MyCustomAttribute a2 = new MyCustomAttribute (null);
1024                         Assert.AreEqual (a1.GetHashCode (), a2.GetHashCode (), "identical arguments");
1025                         Assert.AreEqual (a1.GetHashCode (), a1.TypeId.GetHashCode (), "TypeId");
1026
1027                         MyCustomAttribute a3 = new MyCustomAttribute ("a");
1028                         MyCustomAttribute a4 = new MyCustomAttribute ("b");
1029                         Assert.AreNotEqual (a3.GetHashCode (), a4.GetHashCode (), "non-identical-arguments");
1030
1031                         MyOwnCustomAttribute b1 = new MyOwnCustomAttribute (null);
1032                         Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
1033                 }
1034
1035                 [Test]
1036                 public void GetHashCodeWithOverriddenTypeId ()
1037                 {
1038                         //check for not throwing stack overflow exception
1039                         AttributeWithTypeId a = new AttributeWithTypeId ();
1040                         a.GetHashCode ();
1041                 }
1042
1043
1044                 [Test]
1045                 public void DerivedClassOverrideHasInhertedAttributeFromAbstractRoot ()
1046                 {
1047                         // regression test for #44010
1048                         // we have
1049                         // abstract class Root {
1050                         //   [MyAttribute]
1051                         //   public abstract void Foo ();
1052                         // }
1053                         // abstract class Abs : Root { }
1054                         // class Base : Abs {
1055                         //   public override void  Foo () { }
1056                         // }
1057                         // note that Abs does not itself override Foo.
1058                         var bt = typeof(Base);
1059                         var m = bt.GetMethod ("Foo");
1060                         var attribute = Attribute.GetCustomAttribute (m, typeof (MyAttribute), true);
1061                         Assert.IsNotNull (attribute);
1062                 }
1063
1064                 class ArrayAttribute : Attribute
1065                 {
1066 #pragma warning disable 414
1067                         int[] array;
1068 #pragma warning restore
1069
1070                         public ArrayAttribute (int[] array)
1071                         {
1072                                 this.array = array;
1073                         }
1074                 }
1075
1076                 [Test]
1077                 public void ArrayFieldsEquality ()
1078                 {
1079                         Assert.IsTrue (new ArrayAttribute (new int[] { 1, 2 }).Equals (new ArrayAttribute (new int[] { 1, 2 })));
1080                         Assert.IsFalse (new ArrayAttribute (new int[] { 1, 2 }).Equals (new ArrayAttribute (new int[] { 1, 1 })));
1081                 }
1082         }
1083
1084         namespace ParamNamespace {
1085
1086                 class FooAttribute : Attribute {}
1087                 class BarAttribute : Attribute {}
1088
1089                 class DataAttribute : Attribute {
1090
1091                         public string Data { get; set; }
1092
1093                         public DataAttribute (string data)
1094                         {
1095                                 this.Data = data;
1096                         }
1097                 }
1098
1099                 class UltraBase {
1100
1101                         public virtual void Bar ([Foo] string bar, [Data ("UltraBase.baz")] string baz)
1102                         {
1103                         }
1104                 }
1105
1106                 class Base : UltraBase {
1107
1108                         public override void Bar ([Data ("Base.bar")] string bar, string baz)
1109                         {
1110                         }
1111                 }
1112
1113                 class Derived : Base {
1114
1115                         public override void Bar ([Bar] string bar, [Data ("Derived.baz")] string baz)
1116                         {
1117                         }
1118                 }
1119
1120                 class Multiple {
1121                         public void Bar ([Foo] [Bar] string multiple, [Bar] string bar)
1122                         {
1123                         }
1124                 }
1125         }
1126
1127         [TestFixture]
1128         public class ParamAttributeTest {
1129
1130                 static ParameterInfo GetParameter (Type type, string method_name, string param_name)
1131                 {
1132                         foreach (var method in type.GetMethods ()) {
1133                                 if (method.Name != method_name)
1134                                         continue;
1135
1136                                 foreach (var parameter in method.GetParameters ())
1137                                         if (parameter.Name == param_name)
1138                                                 return parameter;
1139                         }
1140
1141                         return null;
1142                 }
1143
1144                 [Test]
1145                 public void IsDefinedTopLevel ()
1146                 {
1147                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1148
1149                         Assert.IsNotNull (parameter);
1150                         Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.BarAttribute)));
1151                 }
1152
1153                 [Test]
1154                 public void IsDefinedHierarchy ()
1155                 {
1156                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1157
1158                         Assert.IsNotNull (parameter);
1159                         Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.FooAttribute)));
1160                 }
1161
1162                 [Test]
1163                 public void IsDefinedHierarchyMultiple ()
1164                 {
1165                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1166
1167                         Assert.IsNotNull (parameter);
1168                         Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.DataAttribute)));
1169                 }
1170
1171                 [Test]
1172                 public void GetCustomAttributeTopLevel ()
1173                 {
1174                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1175
1176                         Assert.IsNotNull (Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.BarAttribute)));
1177                 }
1178
1179                 [Test]
1180                 public void GetCustomAttributeHierarchy ()
1181                 {
1182                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1183                         var data = (ParamNamespace.DataAttribute) Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.DataAttribute));
1184                         Assert.IsNotNull (data);
1185                         Assert.AreEqual ("Base.bar", data.Data);
1186                 }
1187
1188                 [Test]
1189                 public void GetCustomAttributeHierarchyMultiple ()
1190                 {
1191                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1192                         var data = (ParamNamespace.DataAttribute) Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.DataAttribute));
1193                         Assert.IsNotNull (data);
1194                         Assert.AreEqual ("Derived.baz", data.Data);
1195                 }
1196
1197                 [Test]
1198                 public void GetAllCustomAttributes ()
1199                 {
1200                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1201                         var attributes = (Attribute []) Attribute.GetCustomAttributes (parameter, true);
1202                         Assert.AreEqual (3, attributes.Length);
1203                         Assert.AreEqual (typeof (ParamNamespace.BarAttribute), attributes [0].GetType ());
1204                         Assert.AreEqual (typeof (ParamNamespace.DataAttribute), attributes [1].GetType ());
1205                         Assert.AreEqual (typeof (ParamNamespace.FooAttribute), attributes [2].GetType ());
1206                 }
1207
1208                 [Test]
1209                 public void GetDataCustomAttributes ()
1210                 {
1211                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1212                         var attributes = (ParamNamespace.DataAttribute []) Attribute.GetCustomAttributes (parameter, typeof (ParamNamespace.DataAttribute), true);
1213                         Assert.AreEqual (1, attributes.Length);
1214                         Assert.AreEqual ("Derived.baz", attributes [0].Data);
1215                 }
1216
1217                 [Test]
1218                 public void MultipleParameterAttributes ()
1219                 {
1220                         var parameter = GetParameter (typeof(ParamNamespace.Multiple), "Bar", "multiple");
1221                         var foo = parameter.GetCustomAttribute<ParamNamespace.FooAttribute> ();
1222                         Assert.AreEqual (typeof(ParamNamespace.FooAttribute), foo.GetType ());
1223                         var bar = parameter.GetCustomAttribute<ParamNamespace.BarAttribute> ();
1224                         Assert.AreEqual (typeof(ParamNamespace.BarAttribute), bar.GetType ());
1225                 }
1226
1227                 [Test]
1228                 public void MultipleParameterAttributes2 ()
1229                 {
1230                         var parameter = GetParameter (typeof(ParamNamespace.Multiple), "Bar", "bar");
1231                         var foo = parameter.GetCustomAttribute<ParamNamespace.FooAttribute> ();
1232                         Assert.IsNull (foo);
1233                 }
1234
1235                 [AttributeUsage(AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Class)]
1236                 public class MyCAttr : Attribute {}
1237
1238                 class Base {
1239                         [MyCAttr]
1240                         public override string ToString () { return null; }
1241                 }
1242
1243                 class Derived : Base {
1244                         public override string ToString () { return null; }
1245                 }
1246
1247                 [Test] //one ton of bugs
1248                 public void GetCustomAttributesOnMethodOverride ()
1249                 {
1250                         var m = typeof (Derived).GetMethod ("ToString");
1251                         var attrs = Attribute.GetCustomAttributes (m, true);
1252                         Assert.AreEqual (1, attrs.Length);      
1253                 }
1254
1255                 class EvtBase
1256                 {
1257                         public virtual event EventHandler Event {add {} remove {}}
1258                 }
1259
1260                 class EvtOverride : EvtBase
1261                 {
1262                         [MyCAttr]       
1263                         public override event EventHandler Event {add {} remove {}}
1264                 }
1265
1266                 class EvtChild : EvtOverride
1267                 {
1268                         public override event EventHandler Event {add {} remove {}}
1269                 }
1270
1271                 [Test] //Regression test for #662867
1272                 public void GetCustomAttributesOnEventOverride ()
1273                 {
1274                         var attrs = Attribute.GetCustomAttributes (typeof(EvtChild).GetEvent ("Event"), true);
1275                         Assert.AreEqual (1, attrs.Length);
1276                 }
1277         }
1278 }