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;
16 using System.Reflection.Emit;
18 using System.Runtime.InteropServices;
19 using System.Threading;
21 using NUnit.Framework;
23 namespace MonoTests.System
25 using MonoTests.System.AttributeTestInternals;
27 namespace AttributeTestInternals
29 [AttributeUsage (AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
30 internal class MyCustomAttribute : Attribute
34 public MyCustomAttribute (string info)
46 [AttributeUsage (AttributeTargets.Class)]
47 internal class YourCustomAttribute : Attribute
51 public YourCustomAttribute (int value)
63 [AttributeUsage (AttributeTargets.Class)]
64 internal class UnusedAttribute : Attribute
68 [MyCustomAttribute ("MyBaseClass"), YourCustomAttribute (37)]
69 internal class MyClass
75 public static void ParamsMethod(params object[] args)
80 [MyCustomAttribute ("MyDerivedClass")]
81 internal class MyDerivedClass : MyClass
88 class MyDerivedClassNoAttribute : MyClass
94 public class AttributeTest
97 public void TestIsDefined ()
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");
113 public void IsDefined_PropertyInfo ()
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");
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");
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");
141 public void IsDefined_PropertyInfo_Override ()
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");
153 public void TestGetCustomAttribute ()
156 Type t = typeof(MyDerivedClass);
158 Assert.AreEqual ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute), false))).Info, "#1");
160 Assert.IsNull (((YourCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(YourCustomAttribute), false))), "#2");
162 Assert.AreEqual ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute)))).Info, "#3");
164 Assert.IsNotNull (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)), "#4");
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);
173 public void GetCustomAttribute_PropertyInfo ()
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");
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");
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");
219 public void GetCustomAttribute_PropertyInfo_Override ()
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");
236 /* Test for bug 54518 */
237 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
238 public class PropTestAttribute : Attribute
240 public PropTestAttribute ()
245 public class TestBase
248 public int PropBase1 {
255 public string PropBase2 {
261 public virtual string PropBase3 {
267 public class TestSub : TestBase
270 public int PropSub1 {
276 public string PropSub2 {
281 public override string PropBase3 {
288 public void GetCustomAttributes_Element_Null ()
295 Attribute.GetCustomAttributes ((Assembly) null);
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");
306 Attribute.GetCustomAttributes ((Assembly) null, false);
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");
317 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute));
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");
328 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute), false);
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");
343 Attribute.GetCustomAttributes ((MemberInfo) null);
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");
354 Attribute.GetCustomAttributes ((MemberInfo) null, false);
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");
365 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute));
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");
376 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute), false);
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");
391 Attribute.GetCustomAttributes ((Module) null);
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");
402 Attribute.GetCustomAttributes ((Module) null, false);
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");
413 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute));
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");
424 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute), false);
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");
439 Attribute.GetCustomAttributes ((ParameterInfo) null);
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");
450 Attribute.GetCustomAttributes ((ParameterInfo) null, false);
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");
461 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute));
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");
472 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute), false);
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");
484 public void GetCustomAttributes_PropertyInfo ()
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");
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");
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");
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");
607 public void GetCustomAttributes_PropertyInfo_Override ()
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");
638 public void GetCustomAttributeOK ()
640 Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
641 typeof(DerivedTestCustomAttributeInherit));
642 Assert.IsNotNull (attribute);
646 [ExpectedException (typeof(AmbiguousMatchException))]
647 public void GetCustomAttributeAmbiguous ()
649 Attribute.GetCustomAttribute (typeof(ClassA), typeof(TestCustomAttribute));
653 public void GetCustomAttributeNull ()
655 Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
656 typeof(DerivedTestCustomAttributeMultipleInherit));
657 Assert.IsNull (attribute);
661 public void GetCustomAttributesTypeNoInherit ()
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");
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");
685 public void GetCustomAttributesTypeInherit ()
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");
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");
711 public void TestEquality ()
713 MyCustomAttribute a = new MyCustomAttribute ("one");
714 MyCustomAttribute b = new MyCustomAttribute ("two");
715 MyCustomAttribute c = new MyCustomAttribute ("one");
716 MyCustomAttribute d = a;
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");
728 class UserType : TypeDelegator {
729 public int GetCattr1;
730 public int GetCattr2;
732 public bool lastInherit;
733 public Type lastAttrType;
735 public UserType (Type type) : base (type) {}
737 public override object [] GetCustomAttributes (bool inherit)
740 lastInherit = inherit;
742 return base.GetCustomAttributes (inherit);
745 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
748 lastInherit = inherit;
749 lastAttrType = attributeType;
750 return base.GetCustomAttributes (attributeType, inherit);
753 public override bool IsDefined (Type attributeType, bool inherit)
756 lastInherit = inherit;
757 lastAttrType = attributeType;
758 return base.IsDefined (attributeType, inherit);
763 public void GetCustomAttributeOnUserType ()
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");
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");
778 public void GetCustomAttributeOnMethodInfo ()
780 MemberInfo method = typeof (AttributeTest).GetMethod ("GetCustomAttributeOnMethodInfo");
781 var res = Attribute.GetCustomAttribute (method, typeof (TestAttribute));
783 Assert.IsNotNull (res, "#1");
784 Assert.AreEqual (typeof (TestAttribute), res.GetType (), "#2");
788 public void GetCustomAttributesOnUserType ()
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");
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");
804 public void IsDefinedOnUserType ()
806 UserType type = new UserType (typeof (AttributeTest));
807 var res = Attribute.IsDefined (type, typeof (TestFixtureAttribute));
808 Assert.IsTrue (res, "#1");
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");
818 public void GetCustomAttributeOnNewSreTypes ()
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");
826 var tb = module.DefineType ("ns.type", TypeAttributes.Public);
827 var arr = tb.MakeArrayType ();
828 var ptr = tb.MakePointerType ();
829 var byref = tb.MakeByRefType ();
832 Attribute.GetCustomAttribute (arr, typeof (ObsoleteAttribute));
834 } catch (NotSupportedException) {}
837 Attribute.GetCustomAttribute (ptr, typeof (ObsoleteAttribute));
839 } catch (NotSupportedException) {}
842 Attribute.GetCustomAttribute (byref, typeof (ObsoleteAttribute));
844 } catch (NotSupportedException) {}
848 public void GetCustomAttributeOnBadSreTypes ()
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");
856 var tb = module.DefineType ("ns.type", TypeAttributes.Public);
857 tb.DefineGenericParameters ("T");
858 var ginst = tb.MakeGenericType (typeof (int));
860 Attribute.GetCustomAttribute (ginst, typeof (ObsoleteAttribute));
862 } catch (NotSupportedException) {}
865 [Test] //Regression test for #499569
866 public void GetCattrOnPropertyAndInheritance ()
868 var m = typeof(Sub).GetProperty ("Name");
869 var res = Attribute.GetCustomAttributes (m, typeof(MyAttribute), true);
870 Assert.AreEqual (1, res.Length, "#1");
875 public abstract string Name { get; set; }
881 public override string Name {
889 public override string Name {
895 class MySubAttribute: MyAttribute
899 class MyAttribute: Attribute
903 private int GetAttributeCount (object[] attributes, Type attributeType)
907 foreach (Attribute attribute in attributes) {
908 if (attribute.GetType () == attributeType)
915 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
916 private class TestCustomAttribute : Attribute
920 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = false)]
921 private class DerivedTestCustomAttributeMultiple : TestCustomAttribute
925 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
926 private class DerivedTestCustomAttributeInherit : TestCustomAttribute
930 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = true)]
931 private class DerivedTestCustomAttributeMultipleInherit : TestCustomAttribute
935 [TestCustomAttribute]
936 [DerivedTestCustomAttributeMultiple]
937 [DerivedTestCustomAttributeInherit]
942 [TestCustomAttribute ()]
943 [DerivedTestCustomAttributeMultiple ()]
944 [DerivedTestCustomAttributeMultiple ()]
945 [DerivedTestCustomAttributeMultipleInherit ()]
946 private class ClassB : ClassA
950 [TestCustomAttribute ()]
951 [DerivedTestCustomAttributeMultiple ()]
952 [DerivedTestCustomAttributeMultipleInherit ()]
953 private class ClassC : ClassB
958 public void EmptyNonOverridenGetHashCode ()
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");
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");
970 class MyOwnCustomAttribute : MyCustomAttribute {
972 public MyOwnCustomAttribute (string s)
979 public void NonEmptyNonOverridenGetHashCode ()
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");
986 MyCustomAttribute a3 = new MyCustomAttribute ("a");
987 MyCustomAttribute a4 = new MyCustomAttribute ("b");
988 Assert.AreNotEqual (a3.GetHashCode (), a4.GetHashCode (), "non-identical-arguments");
990 MyOwnCustomAttribute b1 = new MyOwnCustomAttribute (null);
991 Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
995 namespace ParamNamespace {
997 class FooAttribute : Attribute {}
998 class BarAttribute : Attribute {}
1000 class DataAttribute : Attribute {
1002 public string Data { get; set; }
1004 public DataAttribute (string data)
1012 public virtual void Bar ([Foo] string bar, [Data ("UltraBase.baz")] string baz)
1017 class Base : UltraBase {
1019 public override void Bar ([Data ("Base.bar")] string bar, string baz)
1024 class Derived : Base {
1026 public override void Bar ([Bar] string bar, [Data ("Derived.baz")] string baz)
1033 public class ParamAttributeTest {
1035 static ParameterInfo GetParameter (Type type, string method_name, string param_name)
1037 foreach (var method in type.GetMethods ()) {
1038 if (method.Name != method_name)
1041 foreach (var parameter in method.GetParameters ())
1042 if (parameter.Name == param_name)
1050 public void IsDefinedTopLevel ()
1052 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1054 Assert.IsNotNull (parameter);
1055 Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.BarAttribute)));
1059 public void IsDefinedHierarchy ()
1061 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1063 Assert.IsNotNull (parameter);
1064 Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.FooAttribute)));
1068 public void IsDefinedHierarchyMultiple ()
1070 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1072 Assert.IsNotNull (parameter);
1073 Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.DataAttribute)));
1077 public void GetCustomAttributeTopLevel ()
1079 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1081 Assert.IsNotNull (Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.BarAttribute)));
1085 public void GetCustomAttributeHierarchy ()
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);
1094 public void GetCustomAttributeHierarchyMultiple ()
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);
1103 public void GetAllCustomAttributes ()
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 ());
1114 public void GetDataCustomAttributes ()
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);
1122 [AttributeUsage(AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Class)]
1123 public class MyCAttr : Attribute {}
1127 public override string ToString () { return null; }
1130 class Derived : Base {
1131 public override string ToString () { return null; }
1134 [Test] //one ton of bugs
1135 public void GetCustomAttributesOnMethodOverride ()
1137 var m = typeof (Derived).GetMethod ("ToString");
1138 var attrs = Attribute.GetCustomAttributes (m, true);
1139 Assert.AreEqual (1, attrs.Length);
1144 public virtual event EventHandler Event {add {} remove {}}
1147 class EvtOverride : EvtBase
1150 public override event EventHandler Event {add {} remove {}}
1153 class EvtChild : EvtOverride
1155 public override event EventHandler Event {add {} remove {}}
1158 [Test] //Regression test for #662867
1159 public void GetCustomAttributesOnEventOverride ()
1161 var attrs = Attribute.GetCustomAttributes (typeof(EvtChild).GetEvent ("Event"), true);
1162 Assert.AreEqual (1, attrs.Length);