2 // AttributeTest.cs - NUnit Test Cases for the System.Attribute class
5 // Duco Fijma (duco@lorentz.xs4all.nl)
6 // Gonzalo Paniagua (gonzalo@ximian.com)
7 // Gert Driesen (drieseng@users.sourceforge.net)
10 // (c) 2004 Novell, Inc. (http://www.novell.com)
14 using System.Reflection;
15 using System.Runtime.InteropServices;
17 using NUnit.Framework;
19 namespace MonoTests.System
21 using MonoTests.System.AttributeTestInternals;
23 namespace AttributeTestInternals
25 [AttributeUsage (AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
26 internal class MyCustomAttribute : Attribute
30 public MyCustomAttribute (string info)
42 [AttributeUsage (AttributeTargets.Class)]
43 internal class YourCustomAttribute : Attribute
47 public YourCustomAttribute (int value)
59 [AttributeUsage (AttributeTargets.Class)]
60 internal class UnusedAttribute : Attribute
64 [MyCustomAttribute ("MyBaseClass"), YourCustomAttribute (37)]
65 internal class MyClass
71 public static void ParamsMethod(params object[] args)
76 [MyCustomAttribute ("MyDerivedClass")]
77 internal class MyDerivedClass : MyClass
86 public class AttributeTest
89 public void TestIsDefined ()
91 Assert.IsTrue (Attribute.IsDefined (typeof(MyDerivedClass), typeof(MyCustomAttribute)), "#1");
92 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute)), "#2");
93 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute)), "#3");
94 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (MyCustomAttribute), true), "#4");
95 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), true), "#5");
96 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#6");
97 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (MyCustomAttribute), false), "#7");
98 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), false), "#8");
99 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#9");
100 Assert.IsTrue (Attribute.IsDefined (typeof (MyClass).GetMethod ("ParamsMethod").GetParameters () [0], typeof (ParamArrayAttribute), false), "#10");
104 public void IsDefined_PropertyInfo ()
106 PropertyInfo pi = typeof (TestBase).GetProperty ("PropBase3");
107 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#A1");
108 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#A2");
109 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#A3");
110 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#A4");
111 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#A5");
112 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#A6");
114 pi = typeof (TestBase).GetProperty ("PropBase2");
115 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#C1");
116 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#C2");
117 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#C3");
118 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#C4");
119 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#C5");
120 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#C6");
122 pi = typeof (TestSub).GetProperty ("PropBase2");
123 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#D1");
124 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#D2");
125 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#D3");
126 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#D4");
127 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#D5");
128 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#D6");
133 [Category ("NotWorking")] // bug #81797
135 public void IsDefined_PropertyInfo_Override ()
137 PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
139 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
141 Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
143 Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#B2");
145 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
147 Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
149 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#B4");
150 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#B5");
151 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#B6");
155 public void TestGetCustomAttribute ()
158 Type t = typeof(MyDerivedClass);
160 Assert.AreEqual ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute), false))).Info, "#1");
162 Assert.IsNull (((YourCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(YourCustomAttribute), false))), "#2");
164 Assert.AreEqual ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute)))).Info, "#3");
166 Assert.IsNotNull (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)), "#4");
168 Assert.AreEqual (37, ((YourCustomAttribute) (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)))).Value, "#5");
169 } catch (Exception e) {
170 Assert.Fail ("Unexpected exception thrown at i=" + i + " with t=" + t + ". e=" + e);
175 public void GetCustomAttribute_PropertyInfo ()
177 PropertyInfo pi = typeof (TestBase).GetProperty ("PropBase3");
178 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
179 typeof (PropTestAttribute)), "#A1");
180 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
181 typeof (PropTestAttribute), false), "#A2");
182 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
183 typeof (PropTestAttribute), true), "#A3");
184 Assert.IsNull (Attribute.GetCustomAttribute (pi,
185 typeof (ComVisibleAttribute)), "#A4");
186 Assert.IsNull (Attribute.GetCustomAttribute (pi,
187 typeof (ComVisibleAttribute), false), "#A5");
188 Assert.IsNull (Attribute.GetCustomAttribute (pi,
189 typeof (ComVisibleAttribute), true), "#A6");
191 pi = typeof (TestBase).GetProperty ("PropBase2");
192 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
193 typeof (PropTestAttribute)), "#C1");
194 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
195 typeof (PropTestAttribute), false), "#C2");
196 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
197 typeof (PropTestAttribute), true), "#C3");
198 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
199 typeof (ComVisibleAttribute)), "#C4");
200 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
201 typeof (ComVisibleAttribute), false), "#C5");
202 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
203 typeof (ComVisibleAttribute), true), "#C6");
205 pi = typeof (TestSub).GetProperty ("PropBase2");
206 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
207 typeof (PropTestAttribute)), "#D1");
208 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
209 typeof (PropTestAttribute), false), "#D2");
210 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
211 typeof (PropTestAttribute), true), "#D3");
212 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
213 typeof (ComVisibleAttribute)), "#D4");
214 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
215 typeof (ComVisibleAttribute), false), "#D5");
216 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
217 typeof (ComVisibleAttribute), true), "#D6");
221 [Category ("NotWorking")] // bug #81797
222 public void GetCustomAttribute_PropertyInfo_Override ()
224 PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
225 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
226 typeof (PropTestAttribute)), "#B1");
227 Assert.IsNull (Attribute.GetCustomAttribute (pi,
228 typeof (PropTestAttribute), false), "#B2");
229 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
230 typeof (PropTestAttribute), true), "#B3");
231 Assert.IsNull (Attribute.GetCustomAttribute (pi,
232 typeof (ComVisibleAttribute)), "#B4");
233 Assert.IsNull (Attribute.GetCustomAttribute (pi,
234 typeof (ComVisibleAttribute), false), "#B5");
235 Assert.IsNull (Attribute.GetCustomAttribute (pi,
236 typeof (ComVisibleAttribute), true), "#B6");
239 /* Test for bug 54518 */
240 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
241 public class PropTestAttribute : Attribute
243 public PropTestAttribute ()
248 public class TestBase
251 public int PropBase1 {
258 public string PropBase2 {
264 public virtual string PropBase3 {
270 public class TestSub : TestBase
273 public int PropSub1 {
279 public string PropSub2 {
284 public override string PropBase3 {
291 public void GetCustomAttributes_Element_Null ()
298 Attribute.GetCustomAttributes ((Assembly) null);
300 } catch (ArgumentNullException ex) {
301 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
302 Assert.IsNull (ex.InnerException, "#A3");
303 Assert.IsNotNull (ex.Message, "#A4");
304 Assert.IsNotNull (ex.ParamName, "#A5");
305 Assert.AreEqual ("element", ex.ParamName, "#A6");
309 Attribute.GetCustomAttributes ((Assembly) null, false);
311 } catch (ArgumentNullException ex) {
312 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
313 Assert.IsNull (ex.InnerException, "#B3");
314 Assert.IsNotNull (ex.Message, "#B4");
315 Assert.IsNotNull (ex.ParamName, "#B5");
316 Assert.AreEqual ("element", ex.ParamName, "#B6");
320 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute));
322 } catch (ArgumentNullException ex) {
323 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
324 Assert.IsNull (ex.InnerException, "#C3");
325 Assert.IsNotNull (ex.Message, "#C4");
326 Assert.IsNotNull (ex.ParamName, "#C5");
327 Assert.AreEqual ("element", ex.ParamName, "#C6");
331 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute), false);
333 } catch (ArgumentNullException ex) {
334 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
335 Assert.IsNull (ex.InnerException, "#D3");
336 Assert.IsNotNull (ex.Message, "#D4");
337 Assert.IsNotNull (ex.ParamName, "#D5");
338 Assert.AreEqual ("element", ex.ParamName, "#D6");
346 Attribute.GetCustomAttributes ((MemberInfo) null);
348 } catch (ArgumentNullException ex) {
349 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
350 Assert.IsNull (ex.InnerException, "#A3");
351 Assert.IsNotNull (ex.Message, "#A4");
352 Assert.IsNotNull (ex.ParamName, "#A5");
353 Assert.AreEqual ("element", ex.ParamName, "#A6");
357 Attribute.GetCustomAttributes ((MemberInfo) null, false);
359 } catch (ArgumentNullException ex) {
360 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
361 Assert.IsNull (ex.InnerException, "#B3");
362 Assert.IsNotNull (ex.Message, "#B4");
363 Assert.IsNotNull (ex.ParamName, "#B5");
364 Assert.AreEqual ("element", ex.ParamName, "#B6");
368 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute));
370 } catch (ArgumentNullException ex) {
371 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
372 Assert.IsNull (ex.InnerException, "#C3");
373 Assert.IsNotNull (ex.Message, "#C4");
374 Assert.IsNotNull (ex.ParamName, "#C5");
375 Assert.AreEqual ("element", ex.ParamName, "#C6");
379 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute), false);
381 } catch (ArgumentNullException ex) {
382 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
383 Assert.IsNull (ex.InnerException, "#D3");
384 Assert.IsNotNull (ex.Message, "#D4");
385 Assert.IsNotNull (ex.ParamName, "#D5");
386 Assert.AreEqual ("element", ex.ParamName, "#D6");
394 Attribute.GetCustomAttributes ((Module) null);
396 } catch (ArgumentNullException ex) {
397 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
398 Assert.IsNull (ex.InnerException, "#A3");
399 Assert.IsNotNull (ex.Message, "#A4");
400 Assert.IsNotNull (ex.ParamName, "#A5");
401 Assert.AreEqual ("element", ex.ParamName, "#A6");
405 Attribute.GetCustomAttributes ((Module) null, false);
407 } catch (ArgumentNullException ex) {
408 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
409 Assert.IsNull (ex.InnerException, "#B3");
410 Assert.IsNotNull (ex.Message, "#B4");
411 Assert.IsNotNull (ex.ParamName, "#B5");
412 Assert.AreEqual ("element", ex.ParamName, "#B6");
416 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute));
418 } catch (ArgumentNullException ex) {
419 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
420 Assert.IsNull (ex.InnerException, "#C3");
421 Assert.IsNotNull (ex.Message, "#C4");
422 Assert.IsNotNull (ex.ParamName, "#C5");
423 Assert.AreEqual ("element", ex.ParamName, "#C6");
427 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute), false);
429 } catch (ArgumentNullException ex) {
430 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
431 Assert.IsNull (ex.InnerException, "#D3");
432 Assert.IsNotNull (ex.Message, "#D4");
433 Assert.IsNotNull (ex.ParamName, "#D5");
434 Assert.AreEqual ("element", ex.ParamName, "#D6");
442 Attribute.GetCustomAttributes ((ParameterInfo) null);
444 } catch (ArgumentNullException ex) {
445 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
446 Assert.IsNull (ex.InnerException, "#A3");
447 Assert.IsNotNull (ex.Message, "#A4");
448 Assert.IsNotNull (ex.ParamName, "#A5");
449 Assert.AreEqual ("element", ex.ParamName, "#A6");
453 Attribute.GetCustomAttributes ((ParameterInfo) null, false);
455 } catch (ArgumentNullException ex) {
456 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
457 Assert.IsNull (ex.InnerException, "#B3");
458 Assert.IsNotNull (ex.Message, "#B4");
459 Assert.IsNotNull (ex.ParamName, "#B5");
460 Assert.AreEqual ("element", ex.ParamName, "#B6");
464 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute));
466 } catch (ArgumentNullException ex) {
467 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
468 Assert.IsNull (ex.InnerException, "#C3");
469 Assert.IsNotNull (ex.Message, "#C4");
470 Assert.IsNotNull (ex.ParamName, "#C5");
471 Assert.AreEqual ("element", ex.ParamName, "#C6");
475 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute), false);
477 } catch (ArgumentNullException ex) {
478 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
479 Assert.IsNull (ex.InnerException, "#D3");
480 Assert.IsNotNull (ex.Message, "#D4");
481 Assert.IsNotNull (ex.ParamName, "#D5");
482 Assert.AreEqual ("element", ex.ParamName, "#D6");
487 public void GetCustomAttributes_PropertyInfo ()
492 pi = typeof (TestBase).GetProperty ("PropBase3");
493 attrs = Attribute.GetCustomAttributes (pi);
494 Assert.AreEqual (1, attrs.Length, "#A1");
495 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A2");
496 attrs = Attribute.GetCustomAttributes (pi, false);
497 Assert.AreEqual (1, attrs.Length, "#A3");
498 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A4");
499 attrs = Attribute.GetCustomAttributes (pi, true);
500 Assert.AreEqual (1, attrs.Length, "#A5");
501 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A6");
502 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
503 Assert.AreEqual (1, attrs.Length, "#A7");
504 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A8");
505 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
506 Assert.AreEqual (1, attrs.Length, "#A9");
507 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A10");
508 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
509 Assert.AreEqual (1, attrs.Length, "#A11");
510 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A12");
511 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
512 Assert.AreEqual (0, attrs.Length, "#A13");
513 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
514 Assert.AreEqual (0, attrs.Length, "#A14");
515 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
516 Assert.AreEqual (0, attrs.Length, "#A15");
518 pi = typeof (TestBase).GetProperty ("PropBase2");
519 attrs = Attribute.GetCustomAttributes (pi);
520 Assert.AreEqual (2, attrs.Length, "#C1");
521 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C2");
522 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C3");
523 attrs = Attribute.GetCustomAttributes (pi, false);
524 Assert.AreEqual (2, attrs.Length, "#C4");
525 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C5");
526 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C6");
527 attrs = Attribute.GetCustomAttributes (pi, true);
528 Assert.AreEqual (2, attrs.Length, "#C7");
529 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C8");
530 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C9");
531 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
532 Assert.AreEqual (1, attrs.Length, "#C10");
533 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C11");
534 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
535 Assert.AreEqual (1, attrs.Length, "#C12");
536 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C13");
537 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
538 Assert.AreEqual (1, attrs.Length, "#C14");
539 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C15");
540 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
541 Assert.AreEqual (1, attrs.Length, "#C16");
542 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C17");
543 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
544 Assert.AreEqual (1, attrs.Length, "#C18");
545 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C19");
546 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
547 Assert.AreEqual (1, attrs.Length, "#C20");
548 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C21");
550 pi = typeof (TestSub).GetProperty ("PropBase2");
551 attrs = Attribute.GetCustomAttributes (pi);
552 Assert.AreEqual (2, attrs.Length, "#D1");
553 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D2");
554 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D3");
555 attrs = Attribute.GetCustomAttributes (pi, false);
556 Assert.AreEqual (2, attrs.Length, "#D4");
557 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D5");
558 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D6");
559 attrs = Attribute.GetCustomAttributes (pi, true);
560 Assert.AreEqual (2, attrs.Length, "#D7");
561 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D8");
562 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D9");
563 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
564 Assert.AreEqual (1, attrs.Length, "#D10");
565 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D11");
566 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
567 Assert.AreEqual (1, attrs.Length, "#D12");
568 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D13");
569 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
570 Assert.AreEqual (1, attrs.Length, "#D14");
571 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D15");
572 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
573 Assert.AreEqual (1, attrs.Length, "#D16");
574 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D17");
575 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
576 Assert.AreEqual (1, attrs.Length, "#D18");
577 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D19");
578 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
579 Assert.AreEqual (1, attrs.Length, "#D20");
580 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D21");
582 pi = typeof (TestSub).GetProperty ("PropSub1");
583 attrs = Attribute.GetCustomAttributes (pi);
584 Assert.AreEqual (1, attrs.Length, "#E1");
585 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E2");
586 attrs = Attribute.GetCustomAttributes (pi, false);
587 Assert.AreEqual (1, attrs.Length, "#E3");
588 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E4");
589 attrs = Attribute.GetCustomAttributes (pi, true);
590 Assert.AreEqual (1, attrs.Length, "#E5");
591 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E6");
592 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
593 Assert.AreEqual (1, attrs.Length, "#E7");
594 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E8");
595 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
596 Assert.AreEqual (1, attrs.Length, "#E9");
597 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E10");
598 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
599 Assert.AreEqual (1, attrs.Length, "#E11");
600 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E12");
601 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
602 Assert.AreEqual (0, attrs.Length, "#E13");
603 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
604 Assert.AreEqual (0, attrs.Length, "#E14");
605 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
606 Assert.AreEqual (0, attrs.Length, "#D15");
610 [Category ("NotWorking")] // bug #81797
611 public void GetCustomAttributes_PropertyInfo_Override ()
616 pi = typeof (TestSub).GetProperty ("PropBase3");
617 attrs = Attribute.GetCustomAttributes (pi);
618 Assert.AreEqual (1, attrs.Length, "#B1");
619 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B2");
620 attrs = Attribute.GetCustomAttributes (pi, false);
621 Assert.AreEqual (0, attrs.Length, "#B3");
622 attrs = Attribute.GetCustomAttributes (pi, true);
623 Assert.AreEqual (1, attrs.Length, "#B4");
624 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B5");
625 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
626 Assert.AreEqual (1, attrs.Length, "#B6");
627 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B7");
628 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
629 Assert.AreEqual (0, attrs.Length, "#B8");
630 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
631 Assert.AreEqual (1, attrs.Length, "#B9");
632 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B10");
633 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
634 Assert.AreEqual (0, attrs.Length, "#B11");
635 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
636 Assert.AreEqual (0, attrs.Length, "#B12");
637 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
638 Assert.AreEqual (0, attrs.Length, "#B13");
642 public void GetCustomAttributeOK ()
644 Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
645 typeof(DerivedTestCustomAttributeInherit));
646 Assert.IsNotNull (attribute);
650 [ExpectedException (typeof(AmbiguousMatchException))]
651 public void GetCustomAttributeAmbiguous ()
653 Attribute.GetCustomAttribute (typeof(ClassA), typeof(TestCustomAttribute));
657 public void GetCustomAttributeNull ()
659 Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
660 typeof(DerivedTestCustomAttributeMultipleInherit));
661 Assert.IsNull (attribute);
665 public void GetCustomAttributesTypeNoInherit ()
669 attributes = Attribute.GetCustomAttributes (typeof(ClassA), false);
670 Assert.AreEqual (3, attributes.Length, "#A1");
671 Assert.AreEqual (1, GetAttributeCount (attributes,
672 typeof(TestCustomAttribute)), "#A2");
673 Assert.AreEqual (1, GetAttributeCount (attributes,
674 typeof(DerivedTestCustomAttributeMultiple)), "#A3");
675 Assert.AreEqual (1, GetAttributeCount (attributes,
676 typeof(DerivedTestCustomAttributeInherit)), "#A4");
678 attributes = Attribute.GetCustomAttributes (typeof(ClassB), false);
679 Assert.AreEqual (4, attributes.Length, "#B1");
680 Assert.AreEqual (1, GetAttributeCount (attributes,
681 typeof(TestCustomAttribute)), "#B2");
682 Assert.AreEqual (2, GetAttributeCount (attributes,
683 typeof(DerivedTestCustomAttributeMultiple)), "#B3");
684 Assert.AreEqual (1, GetAttributeCount (attributes,
685 typeof(DerivedTestCustomAttributeMultipleInherit)), "#B4");
689 public void GetCustomAttributesTypeInherit ()
693 attributes = Attribute.GetCustomAttributes (typeof(ClassA), true);
694 Assert.AreEqual (3, attributes.Length, "#A1");
695 Assert.AreEqual (1, GetAttributeCount (attributes,
696 typeof(TestCustomAttribute)), "#A2");
697 Assert.AreEqual (1, GetAttributeCount (attributes,
698 typeof(DerivedTestCustomAttributeMultiple)), "#A3");
699 Assert.AreEqual (1, GetAttributeCount (attributes,
700 typeof(DerivedTestCustomAttributeInherit)), "#A4");
702 attributes = Attribute.GetCustomAttributes (typeof(ClassB), true);
703 Assert.AreEqual (5, attributes.Length, "#B1");
704 Assert.AreEqual (1, GetAttributeCount (attributes,
705 typeof(TestCustomAttribute)), "#B2");
706 Assert.AreEqual (2, GetAttributeCount (attributes,
707 typeof(DerivedTestCustomAttributeMultiple)), "#B3");
708 Assert.AreEqual (1, GetAttributeCount (attributes,
709 typeof(DerivedTestCustomAttributeInherit)), "#B4");
710 Assert.AreEqual (1, GetAttributeCount (attributes,
711 typeof(DerivedTestCustomAttributeMultipleInherit)), "#B5");
715 public void TestEquality ()
717 MyCustomAttribute a = new MyCustomAttribute ("one");
718 MyCustomAttribute b = new MyCustomAttribute ("two");
719 MyCustomAttribute c = new MyCustomAttribute ("one");
720 MyCustomAttribute d = a;
722 Assert.IsTrue (a.Equals (c), "#1");
723 Assert.IsTrue (c.Equals (a), "#2");
724 Assert.IsFalse (c.Equals (b), "#3");
725 Assert.IsFalse (b.Equals (a), "#4");
726 Assert.IsFalse (b.Equals (c), "#5");
727 Assert.IsTrue (a.Equals (a), "#6");
728 Assert.IsTrue (a.Equals (d), "#7");
729 Assert.IsFalse (a.Equals (null), "#8");
732 private int GetAttributeCount (object[] attributes, Type attributeType)
736 foreach (Attribute attribute in attributes) {
737 if (attribute.GetType () == attributeType)
744 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
745 private class TestCustomAttribute : Attribute
749 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = false)]
750 private class DerivedTestCustomAttributeMultiple : TestCustomAttribute
754 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
755 private class DerivedTestCustomAttributeInherit : TestCustomAttribute
759 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = true)]
760 private class DerivedTestCustomAttributeMultipleInherit : TestCustomAttribute
764 [TestCustomAttribute]
765 [DerivedTestCustomAttributeMultiple]
766 [DerivedTestCustomAttributeInherit]
771 [TestCustomAttribute ()]
772 [DerivedTestCustomAttributeMultiple ()]
773 [DerivedTestCustomAttributeMultiple ()]
774 [DerivedTestCustomAttributeMultipleInherit ()]
775 private class ClassB : ClassA
779 [TestCustomAttribute ()]
780 [DerivedTestCustomAttributeMultiple ()]
781 [DerivedTestCustomAttributeMultipleInherit ()]
782 private class ClassC : ClassB