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