Merge pull request #1668 from alexanderkyte/bug1856
[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
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                 public void OrderIsImportant ()
840                 {
841                         var custom = typeof (ClassForOrderIsImportant).GetCustomAttributes (false);
842                         Assert.IsTrue (custom [0].GetType () == typeof (YourCustomAttribute));
843                         Assert.IsTrue (custom [1].GetType () == typeof (MyCustomAttribute));
844                         Assert.IsTrue (custom [2].GetType () == typeof (SerializableAttribute));
845                 }
846
847 #if !MONOTOUCH
848                 [Test]
849                 public void GetCustomAttributeOnNewSreTypes ()
850                 {
851                         AssemblyName assemblyName = new AssemblyName ();
852                         assemblyName.Name = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
853                         AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
854                                         assemblyName, AssemblyBuilderAccess.Run);
855                         ModuleBuilder module = assembly.DefineDynamicModule ("module1");
856
857                         var tb = module.DefineType ("ns.type", TypeAttributes.Public);
858                         var arr = tb.MakeArrayType ();
859                         var ptr = tb.MakePointerType ();
860                         var byref = tb.MakeByRefType ();
861
862                         try {
863                                 Attribute.GetCustomAttribute (arr, typeof (ObsoleteAttribute));
864                                 Assert.Fail ("#1");
865                         } catch (NotSupportedException) {}
866
867                         try {
868                                 Attribute.GetCustomAttribute (ptr, typeof (ObsoleteAttribute));
869                                 Assert.Fail ("#2");
870                         } catch (NotSupportedException) {}
871
872                         try {
873                                 Attribute.GetCustomAttribute (byref, typeof (ObsoleteAttribute));
874                                 Assert.Fail ("#3");
875                         } catch (NotSupportedException) {}
876                 }
877
878                 [Test]
879                 public void GetCustomAttributeOnBadSreTypes ()
880                 {
881                         AssemblyName assemblyName = new AssemblyName ();
882                         assemblyName.Name = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
883                         AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
884                                         assemblyName, AssemblyBuilderAccess.Run);
885                         ModuleBuilder module = assembly.DefineDynamicModule ("module1");
886
887                         var tb = module.DefineType ("ns.type", TypeAttributes.Public);
888                         tb.DefineGenericParameters ("T");
889                         var ginst = tb.MakeGenericType (typeof (int));
890                         try {
891                                 Attribute.GetCustomAttribute (ginst, typeof (ObsoleteAttribute));
892                                 Assert.Fail ("#1");
893                         } catch (NotSupportedException) {}
894                 }
895 #endif
896                 [Test] //Regression test for #499569
897                 public void GetCattrOnPropertyAndInheritance ()
898                 {
899                         var m = typeof(Sub).GetProperty ("Name");
900                         var res = Attribute.GetCustomAttributes (m, typeof(MyAttribute), true);
901                         Assert.AreEqual (1, res.Length, "#1");
902                 }
903
904                 abstract class Abs
905                 {
906                         public abstract string Name { get; set; }
907                 }
908                 
909                 class Base: Abs
910                 {
911                         [MyAttribute]
912                         public override string Name {
913                                 get { return ""; }
914                                 set {}
915                         }
916                 }
917                 
918                 class Sub: Base
919                 {
920                         public override string Name {
921                                 get { return ""; }
922                                 set {}
923                         }
924                 }
925                 
926                 class MySubAttribute: MyAttribute
927                 {
928                 }
929                 
930                 class MyAttribute: Attribute
931                 {
932                 }
933
934                 private int GetAttributeCount (object[] attributes, Type attributeType)
935                 {
936                         int counter = 0;
937
938                         foreach (Attribute attribute in attributes) {
939                                 if (attribute.GetType () == attributeType)
940                                         counter++;
941                         }
942
943                         return counter;
944                 }
945
946                 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
947                 private class TestCustomAttribute : Attribute
948                 {
949                 }
950
951                 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = false)]
952                 private class DerivedTestCustomAttributeMultiple : TestCustomAttribute
953                 {
954                 }
955
956                 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
957                 private class DerivedTestCustomAttributeInherit : TestCustomAttribute
958                 {
959                 }
960
961                 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = true)]
962                 private class DerivedTestCustomAttributeMultipleInherit : TestCustomAttribute
963                 {
964                 }
965
966                 [TestCustomAttribute]
967                 [DerivedTestCustomAttributeMultiple]
968                 [DerivedTestCustomAttributeInherit]
969                 private class ClassA
970                 {
971                 }
972
973                 [TestCustomAttribute ()]
974                 [DerivedTestCustomAttributeMultiple ()]
975                 [DerivedTestCustomAttributeMultiple ()]
976                 [DerivedTestCustomAttributeMultipleInherit ()]
977                 private class ClassB : ClassA
978                 {
979                 }
980
981                 [TestCustomAttribute ()]
982                 [DerivedTestCustomAttributeMultiple ()]
983                 [DerivedTestCustomAttributeMultipleInherit ()]
984                 private class ClassC : ClassB
985                 {
986                 }
987
988                 [Test]
989                 public void EmptyNonOverridenGetHashCode ()
990                 {
991                         MyAttribute a1 = new MyAttribute ();
992                         MyAttribute a2 = new MyAttribute ();
993                         Assert.AreEqual (a1.GetHashCode (), a2.GetHashCode (), "identical argument-less");
994                         Assert.AreEqual (a1.GetHashCode (), a1.TypeId.GetHashCode (), "Empty/TypeId");
995
996                         MySubAttribute b1 = new MySubAttribute ();
997                         Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
998                         Assert.AreEqual (b1.GetHashCode (), b1.TypeId.GetHashCode (), "Empty/TypeId/Sub");
999                 }
1000
1001                 class MyOwnCustomAttribute : MyCustomAttribute {
1002
1003                         public MyOwnCustomAttribute (string s)
1004                                 : base (s)
1005                         {
1006                         }
1007                 }
1008
1009                 [Test]
1010                 public void NonEmptyNonOverridenGetHashCode ()
1011                 {
1012                         MyCustomAttribute a1 = new MyCustomAttribute (null);
1013                         MyCustomAttribute a2 = new MyCustomAttribute (null);
1014                         Assert.AreEqual (a1.GetHashCode (), a2.GetHashCode (), "identical arguments");
1015                         Assert.AreEqual (a1.GetHashCode (), a1.TypeId.GetHashCode (), "TypeId");
1016
1017                         MyCustomAttribute a3 = new MyCustomAttribute ("a");
1018                         MyCustomAttribute a4 = new MyCustomAttribute ("b");
1019                         Assert.AreNotEqual (a3.GetHashCode (), a4.GetHashCode (), "non-identical-arguments");
1020
1021                         MyOwnCustomAttribute b1 = new MyOwnCustomAttribute (null);
1022                         Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
1023                 }
1024
1025                 [Test]
1026                 public void GetHashCodeWithOverriddenTypeId ()
1027                 {
1028                         //check for not throwing stack overflow exception
1029                         AttributeWithTypeId a = new AttributeWithTypeId ();
1030                         a.GetHashCode ();
1031                 }
1032
1033                 class ArrayAttribute : Attribute
1034                 {
1035 #pragma warning disable 414
1036                         int[] array;
1037 #pragma warning restore
1038
1039                         public ArrayAttribute (int[] array)
1040                         {
1041                                 this.array = array;
1042                         }
1043                 }
1044
1045                 [Test]
1046                 public void ArrayFieldsEquality ()
1047                 {
1048                         Assert.IsTrue (new ArrayAttribute (new int[] { 1, 2 }).Equals (new ArrayAttribute (new int[] { 1, 2 })));
1049                         Assert.IsFalse (new ArrayAttribute (new int[] { 1, 2 }).Equals (new ArrayAttribute (new int[] { 1, 1 })));
1050                 }
1051         }
1052
1053         namespace ParamNamespace {
1054
1055                 class FooAttribute : Attribute {}
1056                 class BarAttribute : Attribute {}
1057
1058                 class DataAttribute : Attribute {
1059
1060                         public string Data { get; set; }
1061
1062                         public DataAttribute (string data)
1063                         {
1064                                 this.Data = data;
1065                         }
1066                 }
1067
1068                 class UltraBase {
1069
1070                         public virtual void Bar ([Foo] string bar, [Data ("UltraBase.baz")] string baz)
1071                         {
1072                         }
1073                 }
1074
1075                 class Base : UltraBase {
1076
1077                         public override void Bar ([Data ("Base.bar")] string bar, string baz)
1078                         {
1079                         }
1080                 }
1081
1082                 class Derived : Base {
1083
1084                         public override void Bar ([Bar] string bar, [Data ("Derived.baz")] string baz)
1085                         {
1086                         }
1087                 }
1088
1089                 class Multiple {
1090                         public void Bar ([Foo] [Bar] string multiple, [Bar] string bar)
1091                         {
1092                         }
1093                 }
1094         }
1095
1096         [TestFixture]
1097         public class ParamAttributeTest {
1098
1099                 static ParameterInfo GetParameter (Type type, string method_name, string param_name)
1100                 {
1101                         foreach (var method in type.GetMethods ()) {
1102                                 if (method.Name != method_name)
1103                                         continue;
1104
1105                                 foreach (var parameter in method.GetParameters ())
1106                                         if (parameter.Name == param_name)
1107                                                 return parameter;
1108                         }
1109
1110                         return null;
1111                 }
1112
1113                 [Test]
1114                 public void IsDefinedTopLevel ()
1115                 {
1116                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1117
1118                         Assert.IsNotNull (parameter);
1119                         Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.BarAttribute)));
1120                 }
1121
1122                 [Test]
1123                 public void IsDefinedHierarchy ()
1124                 {
1125                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1126
1127                         Assert.IsNotNull (parameter);
1128                         Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.FooAttribute)));
1129                 }
1130
1131                 [Test]
1132                 public void IsDefinedHierarchyMultiple ()
1133                 {
1134                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1135
1136                         Assert.IsNotNull (parameter);
1137                         Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.DataAttribute)));
1138                 }
1139
1140                 [Test]
1141                 public void GetCustomAttributeTopLevel ()
1142                 {
1143                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1144
1145                         Assert.IsNotNull (Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.BarAttribute)));
1146                 }
1147
1148                 [Test]
1149                 public void GetCustomAttributeHierarchy ()
1150                 {
1151                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1152                         var data = (ParamNamespace.DataAttribute) Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.DataAttribute));
1153                         Assert.IsNotNull (data);
1154                         Assert.AreEqual ("Base.bar", data.Data);
1155                 }
1156
1157                 [Test]
1158                 public void GetCustomAttributeHierarchyMultiple ()
1159                 {
1160                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1161                         var data = (ParamNamespace.DataAttribute) Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.DataAttribute));
1162                         Assert.IsNotNull (data);
1163                         Assert.AreEqual ("Derived.baz", data.Data);
1164                 }
1165
1166                 [Test]
1167                 public void GetAllCustomAttributes ()
1168                 {
1169                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1170                         var attributes = (Attribute []) Attribute.GetCustomAttributes (parameter, true);
1171                         Assert.AreEqual (3, attributes.Length);
1172                         Assert.AreEqual (typeof (ParamNamespace.BarAttribute), attributes [0].GetType ());
1173                         Assert.AreEqual (typeof (ParamNamespace.DataAttribute), attributes [1].GetType ());
1174                         Assert.AreEqual (typeof (ParamNamespace.FooAttribute), attributes [2].GetType ());
1175                 }
1176
1177                 [Test]
1178                 public void GetDataCustomAttributes ()
1179                 {
1180                         var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1181                         var attributes = (ParamNamespace.DataAttribute []) Attribute.GetCustomAttributes (parameter, typeof (ParamNamespace.DataAttribute), true);
1182                         Assert.AreEqual (1, attributes.Length);
1183                         Assert.AreEqual ("Derived.baz", attributes [0].Data);
1184                 }
1185
1186                 [Test]
1187                 public void MultipleParameterAttributes ()
1188                 {
1189                         var parameter = GetParameter (typeof(ParamNamespace.Multiple), "Bar", "multiple");
1190                         var foo = parameter.GetCustomAttribute<ParamNamespace.FooAttribute> ();
1191                         Assert.AreEqual (typeof(ParamNamespace.FooAttribute), foo.GetType ());
1192                         var bar = parameter.GetCustomAttribute<ParamNamespace.BarAttribute> ();
1193                         Assert.AreEqual (typeof(ParamNamespace.BarAttribute), bar.GetType ());
1194                 }
1195
1196                 [Test]
1197                 public void MultipleParameterAttributes2 ()
1198                 {
1199                         var parameter = GetParameter (typeof(ParamNamespace.Multiple), "Bar", "bar");
1200                         var foo = parameter.GetCustomAttribute<ParamNamespace.FooAttribute> ();
1201                         Assert.IsNull (foo);
1202                 }
1203
1204                 [AttributeUsage(AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Class)]
1205                 public class MyCAttr : Attribute {}
1206
1207                 class Base {
1208                         [MyCAttr]
1209                         public override string ToString () { return null; }
1210                 }
1211
1212                 class Derived : Base {
1213                         public override string ToString () { return null; }
1214                 }
1215
1216                 [Test] //one ton of bugs
1217                 public void GetCustomAttributesOnMethodOverride ()
1218                 {
1219                         var m = typeof (Derived).GetMethod ("ToString");
1220                         var attrs = Attribute.GetCustomAttributes (m, true);
1221                         Assert.AreEqual (1, attrs.Length);      
1222                 }
1223
1224                 class EvtBase
1225                 {
1226                         public virtual event EventHandler Event {add {} remove {}}
1227                 }
1228
1229                 class EvtOverride : EvtBase
1230                 {
1231                         [MyCAttr]       
1232                         public override event EventHandler Event {add {} remove {}}
1233                 }
1234
1235                 class EvtChild : EvtOverride
1236                 {
1237                         public override event EventHandler Event {add {} remove {}}
1238                 }
1239
1240                 [Test] //Regression test for #662867
1241                 public void GetCustomAttributesOnEventOverride ()
1242                 {
1243                         var attrs = Attribute.GetCustomAttributes (typeof(EvtChild).GetEvent ("Event"), true);
1244                         Assert.AreEqual (1, attrs.Length);
1245                 }
1246         }
1247 }