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 #if !MONOTOUCH && !MOBILE_STATIC
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
92 internal class AttributeWithTypeId : Attribute
94 public override object TypeId {
101 public class AttributeTest
104 public void TestIsDefined ()
106 Assert.IsTrue (Attribute.IsDefined (typeof(MyDerivedClass), typeof(MyCustomAttribute)), "#1");
107 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute)), "#2");
108 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute)), "#3");
109 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (MyCustomAttribute), true), "#4");
110 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), true), "#5");
111 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#6");
112 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (MyCustomAttribute), false), "#7");
113 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), false), "#8");
114 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#9");
115 Assert.IsTrue (Attribute.IsDefined (typeof (MyClass).GetMethod ("ParamsMethod").GetParameters () [0], typeof (ParamArrayAttribute), false), "#10");
116 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClassNoAttribute), typeof (MyCustomAttribute)), "#11");
120 public void IsDefined_PropertyInfo ()
122 PropertyInfo pi = typeof (TestBase).GetProperty ("PropBase3");
123 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#A1");
124 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#A2");
125 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#A3");
126 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#A4");
127 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#A5");
128 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#A6");
130 pi = typeof (TestBase).GetProperty ("PropBase2");
131 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#C1");
132 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#C2");
133 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#C3");
134 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#C4");
135 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#C5");
136 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#C6");
138 pi = typeof (TestSub).GetProperty ("PropBase2");
139 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#D1");
140 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#D2");
141 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#D3");
142 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#D4");
143 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#D5");
144 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#D6");
148 public void IsDefined_PropertyInfo_Override ()
150 PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
151 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
152 Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#B2");
153 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
154 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#B4");
155 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#B5");
156 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#B6");
160 public void TestGetCustomAttribute ()
163 Type t = typeof(MyDerivedClass);
165 Assert.AreEqual ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute), false))).Info, "#1");
167 Assert.IsNull (((YourCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(YourCustomAttribute), false))), "#2");
169 Assert.AreEqual ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute)))).Info, "#3");
171 Assert.IsNotNull (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)), "#4");
173 Assert.AreEqual (37, ((YourCustomAttribute) (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)))).Value, "#5");
174 } catch (Exception e) {
175 Assert.Fail ("Unexpected exception thrown at i=" + i + " with t=" + t + ". e=" + e);
180 public void GetCustomAttribute_PropertyInfo ()
182 PropertyInfo pi = typeof (TestBase).GetProperty ("PropBase3");
183 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
184 typeof (PropTestAttribute)), "#A1");
185 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
186 typeof (PropTestAttribute), false), "#A2");
187 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
188 typeof (PropTestAttribute), true), "#A3");
189 Assert.IsNull (Attribute.GetCustomAttribute (pi,
190 typeof (ComVisibleAttribute)), "#A4");
191 Assert.IsNull (Attribute.GetCustomAttribute (pi,
192 typeof (ComVisibleAttribute), false), "#A5");
193 Assert.IsNull (Attribute.GetCustomAttribute (pi,
194 typeof (ComVisibleAttribute), true), "#A6");
196 pi = typeof (TestBase).GetProperty ("PropBase2");
197 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
198 typeof (PropTestAttribute)), "#C1");
199 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
200 typeof (PropTestAttribute), false), "#C2");
201 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
202 typeof (PropTestAttribute), true), "#C3");
203 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
204 typeof (ComVisibleAttribute)), "#C4");
205 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
206 typeof (ComVisibleAttribute), false), "#C5");
207 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
208 typeof (ComVisibleAttribute), true), "#C6");
210 pi = typeof (TestSub).GetProperty ("PropBase2");
211 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
212 typeof (PropTestAttribute)), "#D1");
213 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
214 typeof (PropTestAttribute), false), "#D2");
215 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
216 typeof (PropTestAttribute), true), "#D3");
217 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
218 typeof (ComVisibleAttribute)), "#D4");
219 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
220 typeof (ComVisibleAttribute), false), "#D5");
221 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
222 typeof (ComVisibleAttribute), true), "#D6");
226 public void GetCustomAttribute_PropertyInfo_Override ()
228 PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
229 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
230 typeof (PropTestAttribute)), "#B1");
231 Assert.IsNull (Attribute.GetCustomAttribute (pi,
232 typeof (PropTestAttribute), false), "#B2");
233 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
234 typeof (PropTestAttribute), true), "#B3");
235 Assert.IsNull (Attribute.GetCustomAttribute (pi,
236 typeof (ComVisibleAttribute)), "#B4");
237 Assert.IsNull (Attribute.GetCustomAttribute (pi,
238 typeof (ComVisibleAttribute), false), "#B5");
239 Assert.IsNull (Attribute.GetCustomAttribute (pi,
240 typeof (ComVisibleAttribute), true), "#B6");
243 /* Test for bug 54518 */
244 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
245 public class PropTestAttribute : Attribute
247 public PropTestAttribute ()
252 public class TestBase
255 public int PropBase1 {
262 public string PropBase2 {
268 public virtual string PropBase3 {
274 public class TestSub : TestBase
277 public int PropSub1 {
283 public string PropSub2 {
288 public override string PropBase3 {
295 public void GetCustomAttributes_Element_Null ()
302 Attribute.GetCustomAttributes ((Assembly) null);
304 } catch (ArgumentNullException ex) {
305 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
306 Assert.IsNull (ex.InnerException, "#A3");
307 Assert.IsNotNull (ex.Message, "#A4");
308 Assert.IsNotNull (ex.ParamName, "#A5");
309 Assert.AreEqual ("element", ex.ParamName, "#A6");
313 Attribute.GetCustomAttributes ((Assembly) null, false);
315 } catch (ArgumentNullException ex) {
316 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
317 Assert.IsNull (ex.InnerException, "#B3");
318 Assert.IsNotNull (ex.Message, "#B4");
319 Assert.IsNotNull (ex.ParamName, "#B5");
320 Assert.AreEqual ("element", ex.ParamName, "#B6");
324 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute));
326 } catch (ArgumentNullException ex) {
327 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
328 Assert.IsNull (ex.InnerException, "#C3");
329 Assert.IsNotNull (ex.Message, "#C4");
330 Assert.IsNotNull (ex.ParamName, "#C5");
331 Assert.AreEqual ("element", ex.ParamName, "#C6");
335 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute), false);
337 } catch (ArgumentNullException ex) {
338 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
339 Assert.IsNull (ex.InnerException, "#D3");
340 Assert.IsNotNull (ex.Message, "#D4");
341 Assert.IsNotNull (ex.ParamName, "#D5");
342 Assert.AreEqual ("element", ex.ParamName, "#D6");
350 Attribute.GetCustomAttributes ((MemberInfo) null);
352 } catch (ArgumentNullException ex) {
353 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
354 Assert.IsNull (ex.InnerException, "#A3");
355 Assert.IsNotNull (ex.Message, "#A4");
356 Assert.IsNotNull (ex.ParamName, "#A5");
357 Assert.AreEqual ("element", ex.ParamName, "#A6");
361 Attribute.GetCustomAttributes ((MemberInfo) null, false);
363 } catch (ArgumentNullException ex) {
364 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
365 Assert.IsNull (ex.InnerException, "#B3");
366 Assert.IsNotNull (ex.Message, "#B4");
367 Assert.IsNotNull (ex.ParamName, "#B5");
368 Assert.AreEqual ("element", ex.ParamName, "#B6");
372 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute));
374 } catch (ArgumentNullException ex) {
375 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
376 Assert.IsNull (ex.InnerException, "#C3");
377 Assert.IsNotNull (ex.Message, "#C4");
378 Assert.IsNotNull (ex.ParamName, "#C5");
379 Assert.AreEqual ("element", ex.ParamName, "#C6");
383 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute), false);
385 } catch (ArgumentNullException ex) {
386 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
387 Assert.IsNull (ex.InnerException, "#D3");
388 Assert.IsNotNull (ex.Message, "#D4");
389 Assert.IsNotNull (ex.ParamName, "#D5");
390 Assert.AreEqual ("element", ex.ParamName, "#D6");
398 Attribute.GetCustomAttributes ((Module) null);
400 } catch (ArgumentNullException ex) {
401 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
402 Assert.IsNull (ex.InnerException, "#A3");
403 Assert.IsNotNull (ex.Message, "#A4");
404 Assert.IsNotNull (ex.ParamName, "#A5");
405 Assert.AreEqual ("element", ex.ParamName, "#A6");
409 Attribute.GetCustomAttributes ((Module) null, false);
411 } catch (ArgumentNullException ex) {
412 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
413 Assert.IsNull (ex.InnerException, "#B3");
414 Assert.IsNotNull (ex.Message, "#B4");
415 Assert.IsNotNull (ex.ParamName, "#B5");
416 Assert.AreEqual ("element", ex.ParamName, "#B6");
420 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute));
422 } catch (ArgumentNullException ex) {
423 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
424 Assert.IsNull (ex.InnerException, "#C3");
425 Assert.IsNotNull (ex.Message, "#C4");
426 Assert.IsNotNull (ex.ParamName, "#C5");
427 Assert.AreEqual ("element", ex.ParamName, "#C6");
431 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute), false);
433 } catch (ArgumentNullException ex) {
434 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
435 Assert.IsNull (ex.InnerException, "#D3");
436 Assert.IsNotNull (ex.Message, "#D4");
437 Assert.IsNotNull (ex.ParamName, "#D5");
438 Assert.AreEqual ("element", ex.ParamName, "#D6");
446 Attribute.GetCustomAttributes ((ParameterInfo) null);
448 } catch (ArgumentNullException ex) {
449 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
450 Assert.IsNull (ex.InnerException, "#A3");
451 Assert.IsNotNull (ex.Message, "#A4");
452 Assert.IsNotNull (ex.ParamName, "#A5");
453 Assert.AreEqual ("element", ex.ParamName, "#A6");
457 Attribute.GetCustomAttributes ((ParameterInfo) null, false);
459 } catch (ArgumentNullException ex) {
460 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
461 Assert.IsNull (ex.InnerException, "#B3");
462 Assert.IsNotNull (ex.Message, "#B4");
463 Assert.IsNotNull (ex.ParamName, "#B5");
464 Assert.AreEqual ("element", ex.ParamName, "#B6");
468 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute));
470 } catch (ArgumentNullException ex) {
471 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
472 Assert.IsNull (ex.InnerException, "#C3");
473 Assert.IsNotNull (ex.Message, "#C4");
474 Assert.IsNotNull (ex.ParamName, "#C5");
475 Assert.AreEqual ("element", ex.ParamName, "#C6");
479 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute), false);
481 } catch (ArgumentNullException ex) {
482 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
483 Assert.IsNull (ex.InnerException, "#D3");
484 Assert.IsNotNull (ex.Message, "#D4");
485 Assert.IsNotNull (ex.ParamName, "#D5");
486 Assert.AreEqual ("element", ex.ParamName, "#D6");
491 public void GetCustomAttributes_PropertyInfo ()
496 pi = typeof (TestBase).GetProperty ("PropBase3");
497 attrs = Attribute.GetCustomAttributes (pi);
498 Assert.AreEqual (1, attrs.Length, "#A1");
499 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A2");
500 attrs = Attribute.GetCustomAttributes (pi, false);
501 Assert.AreEqual (1, attrs.Length, "#A3");
502 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A4");
503 attrs = Attribute.GetCustomAttributes (pi, true);
504 Assert.AreEqual (1, attrs.Length, "#A5");
505 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A6");
506 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
507 Assert.AreEqual (1, attrs.Length, "#A7");
508 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A8");
509 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
510 Assert.AreEqual (1, attrs.Length, "#A9");
511 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A10");
512 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
513 Assert.AreEqual (1, attrs.Length, "#A11");
514 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A12");
515 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
516 Assert.AreEqual (0, attrs.Length, "#A13");
517 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
518 Assert.AreEqual (0, attrs.Length, "#A14");
519 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
520 Assert.AreEqual (0, attrs.Length, "#A15");
522 pi = typeof (TestBase).GetProperty ("PropBase2");
523 attrs = Attribute.GetCustomAttributes (pi);
524 Assert.AreEqual (2, attrs.Length, "#C1");
525 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C2");
526 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C3");
527 attrs = Attribute.GetCustomAttributes (pi, false);
528 Assert.AreEqual (2, attrs.Length, "#C4");
529 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C5");
530 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C6");
531 attrs = Attribute.GetCustomAttributes (pi, true);
532 Assert.AreEqual (2, attrs.Length, "#C7");
533 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C8");
534 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C9");
535 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
536 Assert.AreEqual (1, attrs.Length, "#C10");
537 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C11");
538 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
539 Assert.AreEqual (1, attrs.Length, "#C12");
540 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C13");
541 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
542 Assert.AreEqual (1, attrs.Length, "#C14");
543 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C15");
544 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
545 Assert.AreEqual (1, attrs.Length, "#C16");
546 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C17");
547 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
548 Assert.AreEqual (1, attrs.Length, "#C18");
549 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C19");
550 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
551 Assert.AreEqual (1, attrs.Length, "#C20");
552 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C21");
554 pi = typeof (TestSub).GetProperty ("PropBase2");
555 attrs = Attribute.GetCustomAttributes (pi);
556 Assert.AreEqual (2, attrs.Length, "#D1");
557 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D2");
558 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D3");
559 attrs = Attribute.GetCustomAttributes (pi, false);
560 Assert.AreEqual (2, attrs.Length, "#D4");
561 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D5");
562 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D6");
563 attrs = Attribute.GetCustomAttributes (pi, true);
564 Assert.AreEqual (2, attrs.Length, "#D7");
565 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D8");
566 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D9");
567 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
568 Assert.AreEqual (1, attrs.Length, "#D10");
569 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D11");
570 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
571 Assert.AreEqual (1, attrs.Length, "#D12");
572 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D13");
573 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
574 Assert.AreEqual (1, attrs.Length, "#D14");
575 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D15");
576 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
577 Assert.AreEqual (1, attrs.Length, "#D16");
578 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D17");
579 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
580 Assert.AreEqual (1, attrs.Length, "#D18");
581 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D19");
582 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
583 Assert.AreEqual (1, attrs.Length, "#D20");
584 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D21");
586 pi = typeof (TestSub).GetProperty ("PropSub1");
587 attrs = Attribute.GetCustomAttributes (pi);
588 Assert.AreEqual (1, attrs.Length, "#E1");
589 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E2");
590 attrs = Attribute.GetCustomAttributes (pi, false);
591 Assert.AreEqual (1, attrs.Length, "#E3");
592 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E4");
593 attrs = Attribute.GetCustomAttributes (pi, true);
594 Assert.AreEqual (1, attrs.Length, "#E5");
595 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E6");
596 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
597 Assert.AreEqual (1, attrs.Length, "#E7");
598 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E8");
599 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
600 Assert.AreEqual (1, attrs.Length, "#E9");
601 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E10");
602 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
603 Assert.AreEqual (1, attrs.Length, "#E11");
604 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E12");
605 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
606 Assert.AreEqual (0, attrs.Length, "#E13");
607 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
608 Assert.AreEqual (0, attrs.Length, "#E14");
609 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
610 Assert.AreEqual (0, attrs.Length, "#D15");
614 public void GetCustomAttributes_PropertyInfo_Override ()
619 pi = typeof (TestSub).GetProperty ("PropBase3");
620 attrs = Attribute.GetCustomAttributes (pi);
621 Assert.AreEqual (1, attrs.Length, "#B1");
622 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B2");
623 attrs = Attribute.GetCustomAttributes (pi, false);
624 Assert.AreEqual (0, attrs.Length, "#B3");
625 attrs = Attribute.GetCustomAttributes (pi, true);
626 Assert.AreEqual (1, attrs.Length, "#B4");
627 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B5");
628 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
629 Assert.AreEqual (1, attrs.Length, "#B6");
630 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B7");
631 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
632 Assert.AreEqual (0, attrs.Length, "#B8");
633 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
634 Assert.AreEqual (1, attrs.Length, "#B9");
635 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B10");
636 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
637 Assert.AreEqual (0, attrs.Length, "#B11");
638 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
639 Assert.AreEqual (0, attrs.Length, "#B12");
640 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
641 Assert.AreEqual (0, attrs.Length, "#B13");
645 public void GetCustomAttributeOK ()
647 Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
648 typeof(DerivedTestCustomAttributeInherit));
649 Assert.IsNotNull (attribute);
653 [ExpectedException (typeof(AmbiguousMatchException))]
654 public void GetCustomAttributeAmbiguous ()
656 Attribute.GetCustomAttribute (typeof(ClassA), typeof(TestCustomAttribute));
660 public void GetCustomAttributeNull ()
662 Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
663 typeof(DerivedTestCustomAttributeMultipleInherit));
664 Assert.IsNull (attribute);
668 public void GetCustomAttributesTypeNoInherit ()
672 attributes = Attribute.GetCustomAttributes (typeof(ClassA), false);
673 Assert.AreEqual (3, attributes.Length, "#A1");
674 Assert.AreEqual (1, GetAttributeCount (attributes,
675 typeof(TestCustomAttribute)), "#A2");
676 Assert.AreEqual (1, GetAttributeCount (attributes,
677 typeof(DerivedTestCustomAttributeMultiple)), "#A3");
678 Assert.AreEqual (1, GetAttributeCount (attributes,
679 typeof(DerivedTestCustomAttributeInherit)), "#A4");
681 attributes = Attribute.GetCustomAttributes (typeof(ClassB), false);
682 Assert.AreEqual (4, attributes.Length, "#B1");
683 Assert.AreEqual (1, GetAttributeCount (attributes,
684 typeof(TestCustomAttribute)), "#B2");
685 Assert.AreEqual (2, GetAttributeCount (attributes,
686 typeof(DerivedTestCustomAttributeMultiple)), "#B3");
687 Assert.AreEqual (1, GetAttributeCount (attributes,
688 typeof(DerivedTestCustomAttributeMultipleInherit)), "#B4");
692 public void GetCustomAttributesTypeInherit ()
696 attributes = Attribute.GetCustomAttributes (typeof(ClassA), true);
697 Assert.AreEqual (3, attributes.Length, "#A1");
698 Assert.AreEqual (1, GetAttributeCount (attributes,
699 typeof(TestCustomAttribute)), "#A2");
700 Assert.AreEqual (1, GetAttributeCount (attributes,
701 typeof(DerivedTestCustomAttributeMultiple)), "#A3");
702 Assert.AreEqual (1, GetAttributeCount (attributes,
703 typeof(DerivedTestCustomAttributeInherit)), "#A4");
705 attributes = Attribute.GetCustomAttributes (typeof(ClassB), true);
706 Assert.AreEqual (5, attributes.Length, "#B1");
707 Assert.AreEqual (1, GetAttributeCount (attributes,
708 typeof(TestCustomAttribute)), "#B2");
709 Assert.AreEqual (2, GetAttributeCount (attributes,
710 typeof(DerivedTestCustomAttributeMultiple)), "#B3");
711 Assert.AreEqual (1, GetAttributeCount (attributes,
712 typeof(DerivedTestCustomAttributeInherit)), "#B4");
713 Assert.AreEqual (1, GetAttributeCount (attributes,
714 typeof(DerivedTestCustomAttributeMultipleInherit)), "#B5");
718 public void TestEquality ()
720 MyCustomAttribute a = new MyCustomAttribute ("one");
721 MyCustomAttribute b = new MyCustomAttribute ("two");
722 MyCustomAttribute c = new MyCustomAttribute ("one");
723 MyCustomAttribute d = a;
725 Assert.IsTrue (a.Equals (c), "#1");
726 Assert.IsTrue (c.Equals (a), "#2");
727 Assert.IsFalse (c.Equals (b), "#3");
728 Assert.IsFalse (b.Equals (a), "#4");
729 Assert.IsFalse (b.Equals (c), "#5");
730 Assert.IsTrue (a.Equals (a), "#6");
731 Assert.IsTrue (a.Equals (d), "#7");
732 Assert.IsFalse (a.Equals (null), "#8");
735 class UserType : TypeDelegator {
736 public int GetCattr1;
737 public int GetCattr2;
739 public bool lastInherit;
740 public Type lastAttrType;
742 public UserType (Type type) : base (type) {}
744 public override object [] GetCustomAttributes (bool inherit)
747 lastInherit = inherit;
749 return base.GetCustomAttributes (inherit);
752 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
755 lastInherit = inherit;
756 lastAttrType = attributeType;
757 return base.GetCustomAttributes (attributeType, inherit);
760 public override bool IsDefined (Type attributeType, bool inherit)
763 lastInherit = inherit;
764 lastAttrType = attributeType;
765 return base.IsDefined (attributeType, inherit);
770 public void GetCustomAttributeOnUserType ()
772 UserType type = new UserType (typeof (AttributeTest));
773 var res = Attribute.GetCustomAttribute (type, typeof (TestFixtureAttribute));
774 Assert.IsNotNull (res, "#1");
775 Assert.AreEqual (typeof (TestFixtureAttribute), res.GetType (), "#2");
777 Assert.AreEqual (0, type.IsDef, "#4");
778 Assert.AreEqual (0, type.GetCattr1, "#5");
779 Assert.AreEqual (1, type.GetCattr2, "#6");
780 Assert.IsTrue (type.lastInherit, "#7");
781 Assert.AreEqual (typeof (TestFixtureAttribute), type.lastAttrType, "#8");
785 public void GetCustomAttributeOnMethodInfo ()
787 MemberInfo method = typeof (AttributeTest).GetMethod ("GetCustomAttributeOnMethodInfo");
788 var res = Attribute.GetCustomAttribute (method, typeof (TestAttribute));
790 Assert.IsNotNull (res, "#1");
791 Assert.AreEqual (typeof (TestAttribute), res.GetType (), "#2");
795 public void GetCustomAttributesOnUserType ()
797 UserType type = new UserType (typeof (AttributeTest));
798 var res = Attribute.GetCustomAttributes (type);
799 Assert.IsNotNull (res, "#1");
800 Assert.AreEqual (1, res.Length, "#2");
801 Assert.AreEqual (typeof (TestFixtureAttribute), res [0].GetType (), "#3");
803 Assert.AreEqual (0, type.IsDef, "#4");
804 Assert.AreEqual (0, type.GetCattr1, "#5");
805 Assert.AreEqual (1, type.GetCattr2, "#6");
806 Assert.IsTrue (type.lastInherit, "#7");
807 Assert.AreEqual (typeof (Attribute), type.lastAttrType, "#8");
811 public void IsDefinedOnUserType ()
813 UserType type = new UserType (typeof (AttributeTest));
814 var res = Attribute.IsDefined (type, typeof (TestFixtureAttribute));
815 Assert.IsTrue (res, "#1");
817 Assert.AreEqual (1, type.IsDef, "#4");
818 Assert.AreEqual (0, type.GetCattr1, "#5");
819 Assert.AreEqual (0, type.GetCattr2, "#6");
820 Assert.IsTrue (type.lastInherit, "#7");
821 Assert.AreEqual (typeof (TestFixtureAttribute), type.lastAttrType, "#8");
825 public void IsDefinedForPseudoAttribute ()
827 Assert.IsTrue (Attribute.IsDefined (typeof (object), typeof(SerializableAttribute), true), "#1");
828 Assert.IsFalse (Attribute.IsDefined (typeof (AttributeTest), typeof(SerializableAttribute), true), "#2");
831 [YourCustomAttribute (0)]
833 [MyCustomAttribute ("")]
834 class ClassForOrderIsImportant
839 // The linker removes the serializable attribute
840 [Category ("MobileNotWorking")]
841 public void OrderIsImportant ()
843 var custom = typeof (ClassForOrderIsImportant).GetCustomAttributes (false);
844 Assert.IsTrue (custom [0].GetType () == typeof (YourCustomAttribute));
845 Assert.IsTrue (custom [1].GetType () == typeof (MyCustomAttribute));
846 Assert.IsTrue (custom [2].GetType () == typeof (SerializableAttribute));
849 #if !MONOTOUCH && !MOBILE_STATIC
851 public void GetCustomAttributeOnNewSreTypes ()
853 AssemblyName assemblyName = new AssemblyName ();
854 assemblyName.Name = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
855 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
856 assemblyName, AssemblyBuilderAccess.Run);
857 ModuleBuilder module = assembly.DefineDynamicModule ("module1");
859 var tb = module.DefineType ("ns.type", TypeAttributes.Public);
860 var arr = tb.MakeArrayType ();
861 var ptr = tb.MakePointerType ();
862 var byref = tb.MakeByRefType ();
865 Attribute.GetCustomAttribute (arr, typeof (ObsoleteAttribute));
867 } catch (NotSupportedException) {}
870 Attribute.GetCustomAttribute (ptr, typeof (ObsoleteAttribute));
872 } catch (NotSupportedException) {}
875 Attribute.GetCustomAttribute (byref, typeof (ObsoleteAttribute));
877 } catch (NotSupportedException) {}
881 public void GetCustomAttributeOnBadSreTypes ()
883 AssemblyName assemblyName = new AssemblyName ();
884 assemblyName.Name = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
885 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
886 assemblyName, AssemblyBuilderAccess.Run);
887 ModuleBuilder module = assembly.DefineDynamicModule ("module1");
889 var tb = module.DefineType ("ns.type", TypeAttributes.Public);
890 tb.DefineGenericParameters ("T");
891 var ginst = tb.MakeGenericType (typeof (int));
893 Attribute.GetCustomAttribute (ginst, typeof (ObsoleteAttribute));
895 } catch (NotSupportedException) {}
898 [Test] //Regression test for #499569
899 public void GetCattrOnPropertyAndInheritance ()
901 var m = typeof(Sub).GetProperty ("Name");
902 var res = Attribute.GetCustomAttributes (m, typeof(MyAttribute), true);
903 Assert.AreEqual (1, res.Length, "#1");
909 public abstract void Foo ();
912 abstract class Abs : Root
914 public abstract string Name { get; set; }
920 public override string Name {
925 public override void Foo () { }
930 public override string Name {
936 class MySubAttribute: MyAttribute
940 class MyAttribute: Attribute
944 private int GetAttributeCount (object[] attributes, Type attributeType)
948 foreach (Attribute attribute in attributes) {
949 if (attribute.GetType () == attributeType)
956 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
957 private class TestCustomAttribute : Attribute
961 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = false)]
962 private class DerivedTestCustomAttributeMultiple : TestCustomAttribute
966 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
967 private class DerivedTestCustomAttributeInherit : TestCustomAttribute
971 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = true)]
972 private class DerivedTestCustomAttributeMultipleInherit : TestCustomAttribute
976 [TestCustomAttribute]
977 [DerivedTestCustomAttributeMultiple]
978 [DerivedTestCustomAttributeInherit]
983 [TestCustomAttribute ()]
984 [DerivedTestCustomAttributeMultiple ()]
985 [DerivedTestCustomAttributeMultiple ()]
986 [DerivedTestCustomAttributeMultipleInherit ()]
987 private class ClassB : ClassA
991 [TestCustomAttribute ()]
992 [DerivedTestCustomAttributeMultiple ()]
993 [DerivedTestCustomAttributeMultipleInherit ()]
994 private class ClassC : ClassB
999 public void EmptyNonOverridenGetHashCode ()
1001 MyAttribute a1 = new MyAttribute ();
1002 MyAttribute a2 = new MyAttribute ();
1003 Assert.AreEqual (a1.GetHashCode (), a2.GetHashCode (), "identical argument-less");
1004 Assert.AreEqual (a1.GetHashCode (), a1.TypeId.GetHashCode (), "Empty/TypeId");
1006 MySubAttribute b1 = new MySubAttribute ();
1007 Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
1008 Assert.AreEqual (b1.GetHashCode (), b1.TypeId.GetHashCode (), "Empty/TypeId/Sub");
1011 class MyOwnCustomAttribute : MyCustomAttribute {
1013 public MyOwnCustomAttribute (string s)
1020 public void NonEmptyNonOverridenGetHashCode ()
1022 MyCustomAttribute a1 = new MyCustomAttribute (null);
1023 MyCustomAttribute a2 = new MyCustomAttribute (null);
1024 Assert.AreEqual (a1.GetHashCode (), a2.GetHashCode (), "identical arguments");
1025 Assert.AreEqual (a1.GetHashCode (), a1.TypeId.GetHashCode (), "TypeId");
1027 MyCustomAttribute a3 = new MyCustomAttribute ("a");
1028 MyCustomAttribute a4 = new MyCustomAttribute ("b");
1029 Assert.AreNotEqual (a3.GetHashCode (), a4.GetHashCode (), "non-identical-arguments");
1031 MyOwnCustomAttribute b1 = new MyOwnCustomAttribute (null);
1032 Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
1036 public void GetHashCodeWithOverriddenTypeId ()
1038 //check for not throwing stack overflow exception
1039 AttributeWithTypeId a = new AttributeWithTypeId ();
1045 public void DerivedClassOverrideHasInhertedAttributeFromAbstractRoot ()
1047 // regression test for #44010
1049 // abstract class Root {
1051 // public abstract void Foo ();
1053 // abstract class Abs : Root { }
1054 // class Base : Abs {
1055 // public override void Foo () { }
1057 // note that Abs does not itself override Foo.
1058 var bt = typeof(Base);
1059 var m = bt.GetMethod ("Foo");
1060 var attribute = Attribute.GetCustomAttribute (m, typeof (MyAttribute), true);
1061 Assert.IsNotNull (attribute);
1064 class ArrayAttribute : Attribute
1066 #pragma warning disable 414
1068 #pragma warning restore
1070 public ArrayAttribute (int[] array)
1077 public void ArrayFieldsEquality ()
1079 Assert.IsTrue (new ArrayAttribute (new int[] { 1, 2 }).Equals (new ArrayAttribute (new int[] { 1, 2 })));
1080 Assert.IsFalse (new ArrayAttribute (new int[] { 1, 2 }).Equals (new ArrayAttribute (new int[] { 1, 1 })));
1084 namespace ParamNamespace {
1086 class FooAttribute : Attribute {}
1087 class BarAttribute : Attribute {}
1089 class DataAttribute : Attribute {
1091 public string Data { get; set; }
1093 public DataAttribute (string data)
1101 public virtual void Bar ([Foo] string bar, [Data ("UltraBase.baz")] string baz)
1106 class Base : UltraBase {
1108 public override void Bar ([Data ("Base.bar")] string bar, string baz)
1113 class Derived : Base {
1115 public override void Bar ([Bar] string bar, [Data ("Derived.baz")] string baz)
1121 public void Bar ([Foo] [Bar] string multiple, [Bar] string bar)
1128 public class ParamAttributeTest {
1130 static ParameterInfo GetParameter (Type type, string method_name, string param_name)
1132 foreach (var method in type.GetMethods ()) {
1133 if (method.Name != method_name)
1136 foreach (var parameter in method.GetParameters ())
1137 if (parameter.Name == param_name)
1145 public void IsDefinedTopLevel ()
1147 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1149 Assert.IsNotNull (parameter);
1150 Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.BarAttribute)));
1154 public void IsDefinedHierarchy ()
1156 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1158 Assert.IsNotNull (parameter);
1159 Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.FooAttribute)));
1163 public void IsDefinedHierarchyMultiple ()
1165 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1167 Assert.IsNotNull (parameter);
1168 Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.DataAttribute)));
1172 public void GetCustomAttributeTopLevel ()
1174 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1176 Assert.IsNotNull (Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.BarAttribute)));
1180 public void GetCustomAttributeHierarchy ()
1182 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1183 var data = (ParamNamespace.DataAttribute) Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.DataAttribute));
1184 Assert.IsNotNull (data);
1185 Assert.AreEqual ("Base.bar", data.Data);
1189 public void GetCustomAttributeHierarchyMultiple ()
1191 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1192 var data = (ParamNamespace.DataAttribute) Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.DataAttribute));
1193 Assert.IsNotNull (data);
1194 Assert.AreEqual ("Derived.baz", data.Data);
1198 public void GetAllCustomAttributes ()
1200 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1201 var attributes = (Attribute []) Attribute.GetCustomAttributes (parameter, true);
1202 Assert.AreEqual (3, attributes.Length);
1203 Assert.AreEqual (typeof (ParamNamespace.BarAttribute), attributes [0].GetType ());
1204 Assert.AreEqual (typeof (ParamNamespace.DataAttribute), attributes [1].GetType ());
1205 Assert.AreEqual (typeof (ParamNamespace.FooAttribute), attributes [2].GetType ());
1209 public void GetDataCustomAttributes ()
1211 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1212 var attributes = (ParamNamespace.DataAttribute []) Attribute.GetCustomAttributes (parameter, typeof (ParamNamespace.DataAttribute), true);
1213 Assert.AreEqual (1, attributes.Length);
1214 Assert.AreEqual ("Derived.baz", attributes [0].Data);
1218 public void MultipleParameterAttributes ()
1220 var parameter = GetParameter (typeof(ParamNamespace.Multiple), "Bar", "multiple");
1221 var foo = parameter.GetCustomAttribute<ParamNamespace.FooAttribute> ();
1222 Assert.AreEqual (typeof(ParamNamespace.FooAttribute), foo.GetType ());
1223 var bar = parameter.GetCustomAttribute<ParamNamespace.BarAttribute> ();
1224 Assert.AreEqual (typeof(ParamNamespace.BarAttribute), bar.GetType ());
1228 public void MultipleParameterAttributes2 ()
1230 var parameter = GetParameter (typeof(ParamNamespace.Multiple), "Bar", "bar");
1231 var foo = parameter.GetCustomAttribute<ParamNamespace.FooAttribute> ();
1232 Assert.IsNull (foo);
1235 [AttributeUsage(AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Class)]
1236 public class MyCAttr : Attribute {}
1240 public override string ToString () { return null; }
1243 class Derived : Base {
1244 public override string ToString () { return null; }
1247 [Test] //one ton of bugs
1248 public void GetCustomAttributesOnMethodOverride ()
1250 var m = typeof (Derived).GetMethod ("ToString");
1251 var attrs = Attribute.GetCustomAttributes (m, true);
1252 Assert.AreEqual (1, attrs.Length);
1257 public virtual event EventHandler Event {add {} remove {}}
1260 class EvtOverride : EvtBase
1263 public override event EventHandler Event {add {} remove {}}
1266 class EvtChild : EvtOverride
1268 public override event EventHandler Event {add {} remove {}}
1271 [Test] //Regression test for #662867
1272 public void GetCustomAttributesOnEventOverride ()
1274 var attrs = Attribute.GetCustomAttributes (typeof(EvtChild).GetEvent ("Event"), true);
1275 Assert.AreEqual (1, attrs.Length);