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.Reflection.Emit;
16 using System.Runtime.InteropServices;
17 using System.Threading;
19 using NUnit.Framework;
21 namespace MonoTests.System
23 using MonoTests.System.AttributeTestInternals;
25 namespace AttributeTestInternals
27 [AttributeUsage (AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
28 internal class MyCustomAttribute : Attribute
32 public MyCustomAttribute (string info)
44 [AttributeUsage (AttributeTargets.Class)]
45 internal class YourCustomAttribute : Attribute
49 public YourCustomAttribute (int value)
61 [AttributeUsage (AttributeTargets.Class)]
62 internal class UnusedAttribute : Attribute
66 [MyCustomAttribute ("MyBaseClass"), YourCustomAttribute (37)]
67 internal class MyClass
73 public static void ParamsMethod(params object[] args)
78 [MyCustomAttribute ("MyDerivedClass")]
79 internal class MyDerivedClass : MyClass
88 public class AttributeTest
91 public void TestIsDefined ()
93 Assert.IsTrue (Attribute.IsDefined (typeof(MyDerivedClass), typeof(MyCustomAttribute)), "#1");
94 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute)), "#2");
95 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute)), "#3");
96 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (MyCustomAttribute), true), "#4");
97 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), true), "#5");
98 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#6");
99 Assert.IsTrue (Attribute.IsDefined (typeof (MyDerivedClass), typeof (MyCustomAttribute), false), "#7");
100 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), false), "#8");
101 Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#9");
102 Assert.IsTrue (Attribute.IsDefined (typeof (MyClass).GetMethod ("ParamsMethod").GetParameters () [0], typeof (ParamArrayAttribute), false), "#10");
106 public void IsDefined_PropertyInfo ()
108 PropertyInfo pi = typeof (TestBase).GetProperty ("PropBase3");
109 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#A1");
110 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#A2");
111 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#A3");
112 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#A4");
113 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#A5");
114 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#A6");
116 pi = typeof (TestBase).GetProperty ("PropBase2");
117 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#C1");
118 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#C2");
119 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#C3");
120 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#C4");
121 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#C5");
122 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#C6");
124 pi = typeof (TestSub).GetProperty ("PropBase2");
125 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#D1");
126 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#D2");
127 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#D3");
128 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#D4");
129 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#D5");
130 Assert.IsTrue (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#D6");
134 public void IsDefined_PropertyInfo_Override ()
136 PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
138 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
140 Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
142 Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#B2");
144 Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
146 Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
148 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#B4");
149 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#B5");
150 Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#B6");
154 public void TestGetCustomAttribute ()
157 Type t = typeof(MyDerivedClass);
159 Assert.AreEqual ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute), false))).Info, "#1");
161 Assert.IsNull (((YourCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(YourCustomAttribute), false))), "#2");
163 Assert.AreEqual ("MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute (typeof(MyDerivedClass), typeof(MyCustomAttribute)))).Info, "#3");
165 Assert.IsNotNull (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)), "#4");
167 Assert.AreEqual (37, ((YourCustomAttribute) (Attribute.GetCustomAttribute (t, typeof(YourCustomAttribute)))).Value, "#5");
168 } catch (Exception e) {
169 Assert.Fail ("Unexpected exception thrown at i=" + i + " with t=" + t + ". e=" + e);
174 public void GetCustomAttribute_PropertyInfo ()
176 PropertyInfo pi = typeof (TestBase).GetProperty ("PropBase3");
177 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
178 typeof (PropTestAttribute)), "#A1");
179 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
180 typeof (PropTestAttribute), false), "#A2");
181 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
182 typeof (PropTestAttribute), true), "#A3");
183 Assert.IsNull (Attribute.GetCustomAttribute (pi,
184 typeof (ComVisibleAttribute)), "#A4");
185 Assert.IsNull (Attribute.GetCustomAttribute (pi,
186 typeof (ComVisibleAttribute), false), "#A5");
187 Assert.IsNull (Attribute.GetCustomAttribute (pi,
188 typeof (ComVisibleAttribute), true), "#A6");
190 pi = typeof (TestBase).GetProperty ("PropBase2");
191 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
192 typeof (PropTestAttribute)), "#C1");
193 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
194 typeof (PropTestAttribute), false), "#C2");
195 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
196 typeof (PropTestAttribute), true), "#C3");
197 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
198 typeof (ComVisibleAttribute)), "#C4");
199 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
200 typeof (ComVisibleAttribute), false), "#C5");
201 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
202 typeof (ComVisibleAttribute), true), "#C6");
204 pi = typeof (TestSub).GetProperty ("PropBase2");
205 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
206 typeof (PropTestAttribute)), "#D1");
207 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
208 typeof (PropTestAttribute), false), "#D2");
209 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
210 typeof (PropTestAttribute), true), "#D3");
211 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
212 typeof (ComVisibleAttribute)), "#D4");
213 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
214 typeof (ComVisibleAttribute), false), "#D5");
215 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
216 typeof (ComVisibleAttribute), true), "#D6");
220 public void GetCustomAttribute_PropertyInfo_Override ()
222 PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
223 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
224 typeof (PropTestAttribute)), "#B1");
225 Assert.IsNull (Attribute.GetCustomAttribute (pi,
226 typeof (PropTestAttribute), false), "#B2");
227 Assert.IsNotNull (Attribute.GetCustomAttribute (pi,
228 typeof (PropTestAttribute), true), "#B3");
229 Assert.IsNull (Attribute.GetCustomAttribute (pi,
230 typeof (ComVisibleAttribute)), "#B4");
231 Assert.IsNull (Attribute.GetCustomAttribute (pi,
232 typeof (ComVisibleAttribute), false), "#B5");
233 Assert.IsNull (Attribute.GetCustomAttribute (pi,
234 typeof (ComVisibleAttribute), true), "#B6");
237 /* Test for bug 54518 */
238 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
239 public class PropTestAttribute : Attribute
241 public PropTestAttribute ()
246 public class TestBase
249 public int PropBase1 {
256 public string PropBase2 {
262 public virtual string PropBase3 {
268 public class TestSub : TestBase
271 public int PropSub1 {
277 public string PropSub2 {
282 public override string PropBase3 {
289 public void GetCustomAttributes_Element_Null ()
296 Attribute.GetCustomAttributes ((Assembly) null);
298 } catch (ArgumentNullException ex) {
299 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
300 Assert.IsNull (ex.InnerException, "#A3");
301 Assert.IsNotNull (ex.Message, "#A4");
302 Assert.IsNotNull (ex.ParamName, "#A5");
303 Assert.AreEqual ("element", ex.ParamName, "#A6");
307 Attribute.GetCustomAttributes ((Assembly) null, false);
309 } catch (ArgumentNullException ex) {
310 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
311 Assert.IsNull (ex.InnerException, "#B3");
312 Assert.IsNotNull (ex.Message, "#B4");
313 Assert.IsNotNull (ex.ParamName, "#B5");
314 Assert.AreEqual ("element", ex.ParamName, "#B6");
318 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute));
320 } catch (ArgumentNullException ex) {
321 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
322 Assert.IsNull (ex.InnerException, "#C3");
323 Assert.IsNotNull (ex.Message, "#C4");
324 Assert.IsNotNull (ex.ParamName, "#C5");
325 Assert.AreEqual ("element", ex.ParamName, "#C6");
329 Attribute.GetCustomAttributes ((Assembly) null, typeof (PropTestAttribute), false);
331 } catch (ArgumentNullException ex) {
332 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
333 Assert.IsNull (ex.InnerException, "#D3");
334 Assert.IsNotNull (ex.Message, "#D4");
335 Assert.IsNotNull (ex.ParamName, "#D5");
336 Assert.AreEqual ("element", ex.ParamName, "#D6");
344 Attribute.GetCustomAttributes ((MemberInfo) null);
346 } catch (ArgumentNullException ex) {
347 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
348 Assert.IsNull (ex.InnerException, "#A3");
349 Assert.IsNotNull (ex.Message, "#A4");
350 Assert.IsNotNull (ex.ParamName, "#A5");
351 Assert.AreEqual ("element", ex.ParamName, "#A6");
355 Attribute.GetCustomAttributes ((MemberInfo) null, false);
357 } catch (ArgumentNullException ex) {
358 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
359 Assert.IsNull (ex.InnerException, "#B3");
360 Assert.IsNotNull (ex.Message, "#B4");
361 Assert.IsNotNull (ex.ParamName, "#B5");
362 Assert.AreEqual ("element", ex.ParamName, "#B6");
366 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute));
368 } catch (ArgumentNullException ex) {
369 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
370 Assert.IsNull (ex.InnerException, "#C3");
371 Assert.IsNotNull (ex.Message, "#C4");
372 Assert.IsNotNull (ex.ParamName, "#C5");
373 Assert.AreEqual ("element", ex.ParamName, "#C6");
377 Attribute.GetCustomAttributes ((MemberInfo) null, typeof (PropTestAttribute), false);
379 } catch (ArgumentNullException ex) {
380 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
381 Assert.IsNull (ex.InnerException, "#D3");
382 Assert.IsNotNull (ex.Message, "#D4");
383 Assert.IsNotNull (ex.ParamName, "#D5");
384 Assert.AreEqual ("element", ex.ParamName, "#D6");
392 Attribute.GetCustomAttributes ((Module) null);
394 } catch (ArgumentNullException ex) {
395 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
396 Assert.IsNull (ex.InnerException, "#A3");
397 Assert.IsNotNull (ex.Message, "#A4");
398 Assert.IsNotNull (ex.ParamName, "#A5");
399 Assert.AreEqual ("element", ex.ParamName, "#A6");
403 Attribute.GetCustomAttributes ((Module) null, false);
405 } catch (ArgumentNullException ex) {
406 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
407 Assert.IsNull (ex.InnerException, "#B3");
408 Assert.IsNotNull (ex.Message, "#B4");
409 Assert.IsNotNull (ex.ParamName, "#B5");
410 Assert.AreEqual ("element", ex.ParamName, "#B6");
414 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute));
416 } catch (ArgumentNullException ex) {
417 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
418 Assert.IsNull (ex.InnerException, "#C3");
419 Assert.IsNotNull (ex.Message, "#C4");
420 Assert.IsNotNull (ex.ParamName, "#C5");
421 Assert.AreEqual ("element", ex.ParamName, "#C6");
425 Attribute.GetCustomAttributes ((Module) null, typeof (PropTestAttribute), false);
427 } catch (ArgumentNullException ex) {
428 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
429 Assert.IsNull (ex.InnerException, "#D3");
430 Assert.IsNotNull (ex.Message, "#D4");
431 Assert.IsNotNull (ex.ParamName, "#D5");
432 Assert.AreEqual ("element", ex.ParamName, "#D6");
440 Attribute.GetCustomAttributes ((ParameterInfo) null);
442 } catch (ArgumentNullException ex) {
443 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
444 Assert.IsNull (ex.InnerException, "#A3");
445 Assert.IsNotNull (ex.Message, "#A4");
446 Assert.IsNotNull (ex.ParamName, "#A5");
447 Assert.AreEqual ("element", ex.ParamName, "#A6");
451 Attribute.GetCustomAttributes ((ParameterInfo) null, false);
453 } catch (ArgumentNullException ex) {
454 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
455 Assert.IsNull (ex.InnerException, "#B3");
456 Assert.IsNotNull (ex.Message, "#B4");
457 Assert.IsNotNull (ex.ParamName, "#B5");
458 Assert.AreEqual ("element", ex.ParamName, "#B6");
462 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute));
464 } catch (ArgumentNullException ex) {
465 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
466 Assert.IsNull (ex.InnerException, "#C3");
467 Assert.IsNotNull (ex.Message, "#C4");
468 Assert.IsNotNull (ex.ParamName, "#C5");
469 Assert.AreEqual ("element", ex.ParamName, "#C6");
473 Attribute.GetCustomAttributes ((ParameterInfo) null, typeof (PropTestAttribute), false);
475 } catch (ArgumentNullException ex) {
476 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
477 Assert.IsNull (ex.InnerException, "#D3");
478 Assert.IsNotNull (ex.Message, "#D4");
479 Assert.IsNotNull (ex.ParamName, "#D5");
480 Assert.AreEqual ("element", ex.ParamName, "#D6");
485 public void GetCustomAttributes_PropertyInfo ()
490 pi = typeof (TestBase).GetProperty ("PropBase3");
491 attrs = Attribute.GetCustomAttributes (pi);
492 Assert.AreEqual (1, attrs.Length, "#A1");
493 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A2");
494 attrs = Attribute.GetCustomAttributes (pi, false);
495 Assert.AreEqual (1, attrs.Length, "#A3");
496 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A4");
497 attrs = Attribute.GetCustomAttributes (pi, true);
498 Assert.AreEqual (1, attrs.Length, "#A5");
499 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A6");
500 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
501 Assert.AreEqual (1, attrs.Length, "#A7");
502 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A8");
503 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
504 Assert.AreEqual (1, attrs.Length, "#A9");
505 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A10");
506 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
507 Assert.AreEqual (1, attrs.Length, "#A11");
508 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#A12");
509 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
510 Assert.AreEqual (0, attrs.Length, "#A13");
511 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
512 Assert.AreEqual (0, attrs.Length, "#A14");
513 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
514 Assert.AreEqual (0, attrs.Length, "#A15");
516 pi = typeof (TestBase).GetProperty ("PropBase2");
517 attrs = Attribute.GetCustomAttributes (pi);
518 Assert.AreEqual (2, attrs.Length, "#C1");
519 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C2");
520 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C3");
521 attrs = Attribute.GetCustomAttributes (pi, false);
522 Assert.AreEqual (2, attrs.Length, "#C4");
523 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C5");
524 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C6");
525 attrs = Attribute.GetCustomAttributes (pi, true);
526 Assert.AreEqual (2, attrs.Length, "#C7");
527 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#C8");
528 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#C9");
529 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
530 Assert.AreEqual (1, attrs.Length, "#C10");
531 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C11");
532 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
533 Assert.AreEqual (1, attrs.Length, "#C12");
534 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C13");
535 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
536 Assert.AreEqual (1, attrs.Length, "#C14");
537 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#C15");
538 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
539 Assert.AreEqual (1, attrs.Length, "#C16");
540 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C17");
541 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
542 Assert.AreEqual (1, attrs.Length, "#C18");
543 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C19");
544 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
545 Assert.AreEqual (1, attrs.Length, "#C20");
546 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#C21");
548 pi = typeof (TestSub).GetProperty ("PropBase2");
549 attrs = Attribute.GetCustomAttributes (pi);
550 Assert.AreEqual (2, attrs.Length, "#D1");
551 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D2");
552 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D3");
553 attrs = Attribute.GetCustomAttributes (pi, false);
554 Assert.AreEqual (2, attrs.Length, "#D4");
555 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D5");
556 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D6");
557 attrs = Attribute.GetCustomAttributes (pi, true);
558 Assert.AreEqual (2, attrs.Length, "#D7");
559 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (PropTestAttribute)), "#D8");
560 Assert.AreEqual (1, GetAttributeCount (attrs, typeof (ComVisibleAttribute)), "#D9");
561 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
562 Assert.AreEqual (1, attrs.Length, "#D10");
563 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D11");
564 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
565 Assert.AreEqual (1, attrs.Length, "#D12");
566 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D13");
567 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
568 Assert.AreEqual (1, attrs.Length, "#D14");
569 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#D15");
570 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
571 Assert.AreEqual (1, attrs.Length, "#D16");
572 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D17");
573 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
574 Assert.AreEqual (1, attrs.Length, "#D18");
575 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D19");
576 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
577 Assert.AreEqual (1, attrs.Length, "#D20");
578 Assert.AreEqual (typeof (ComVisibleAttribute), attrs [0].GetType (), "#D21");
580 pi = typeof (TestSub).GetProperty ("PropSub1");
581 attrs = Attribute.GetCustomAttributes (pi);
582 Assert.AreEqual (1, attrs.Length, "#E1");
583 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E2");
584 attrs = Attribute.GetCustomAttributes (pi, false);
585 Assert.AreEqual (1, attrs.Length, "#E3");
586 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E4");
587 attrs = Attribute.GetCustomAttributes (pi, true);
588 Assert.AreEqual (1, attrs.Length, "#E5");
589 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E6");
590 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
591 Assert.AreEqual (1, attrs.Length, "#E7");
592 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E8");
593 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
594 Assert.AreEqual (1, attrs.Length, "#E9");
595 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E10");
596 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
597 Assert.AreEqual (1, attrs.Length, "#E11");
598 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#E12");
599 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
600 Assert.AreEqual (0, attrs.Length, "#E13");
601 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
602 Assert.AreEqual (0, attrs.Length, "#E14");
603 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
604 Assert.AreEqual (0, attrs.Length, "#D15");
608 public void GetCustomAttributes_PropertyInfo_Override ()
613 pi = typeof (TestSub).GetProperty ("PropBase3");
614 attrs = Attribute.GetCustomAttributes (pi);
615 Assert.AreEqual (1, attrs.Length, "#B1");
616 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B2");
617 attrs = Attribute.GetCustomAttributes (pi, false);
618 Assert.AreEqual (0, attrs.Length, "#B3");
619 attrs = Attribute.GetCustomAttributes (pi, true);
620 Assert.AreEqual (1, attrs.Length, "#B4");
621 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B5");
622 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute));
623 Assert.AreEqual (1, attrs.Length, "#B6");
624 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B7");
625 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), false);
626 Assert.AreEqual (0, attrs.Length, "#B8");
627 attrs = Attribute.GetCustomAttributes (pi, typeof (PropTestAttribute), true);
628 Assert.AreEqual (1, attrs.Length, "#B9");
629 Assert.AreEqual (typeof (PropTestAttribute), attrs [0].GetType (), "#B10");
630 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute));
631 Assert.AreEqual (0, attrs.Length, "#B11");
632 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), false);
633 Assert.AreEqual (0, attrs.Length, "#B12");
634 attrs = Attribute.GetCustomAttributes (pi, typeof (ComVisibleAttribute), true);
635 Assert.AreEqual (0, attrs.Length, "#B13");
639 public void GetCustomAttributeOK ()
641 Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
642 typeof(DerivedTestCustomAttributeInherit));
643 Assert.IsNotNull (attribute);
647 [ExpectedException (typeof(AmbiguousMatchException))]
648 public void GetCustomAttributeAmbiguous ()
650 Attribute.GetCustomAttribute (typeof(ClassA), typeof(TestCustomAttribute));
654 public void GetCustomAttributeNull ()
656 Attribute attribute = Attribute.GetCustomAttribute (typeof(ClassA),
657 typeof(DerivedTestCustomAttributeMultipleInherit));
658 Assert.IsNull (attribute);
662 public void GetCustomAttributesTypeNoInherit ()
666 attributes = Attribute.GetCustomAttributes (typeof(ClassA), false);
667 Assert.AreEqual (3, attributes.Length, "#A1");
668 Assert.AreEqual (1, GetAttributeCount (attributes,
669 typeof(TestCustomAttribute)), "#A2");
670 Assert.AreEqual (1, GetAttributeCount (attributes,
671 typeof(DerivedTestCustomAttributeMultiple)), "#A3");
672 Assert.AreEqual (1, GetAttributeCount (attributes,
673 typeof(DerivedTestCustomAttributeInherit)), "#A4");
675 attributes = Attribute.GetCustomAttributes (typeof(ClassB), false);
676 Assert.AreEqual (4, attributes.Length, "#B1");
677 Assert.AreEqual (1, GetAttributeCount (attributes,
678 typeof(TestCustomAttribute)), "#B2");
679 Assert.AreEqual (2, GetAttributeCount (attributes,
680 typeof(DerivedTestCustomAttributeMultiple)), "#B3");
681 Assert.AreEqual (1, GetAttributeCount (attributes,
682 typeof(DerivedTestCustomAttributeMultipleInherit)), "#B4");
686 public void GetCustomAttributesTypeInherit ()
690 attributes = Attribute.GetCustomAttributes (typeof(ClassA), true);
691 Assert.AreEqual (3, attributes.Length, "#A1");
692 Assert.AreEqual (1, GetAttributeCount (attributes,
693 typeof(TestCustomAttribute)), "#A2");
694 Assert.AreEqual (1, GetAttributeCount (attributes,
695 typeof(DerivedTestCustomAttributeMultiple)), "#A3");
696 Assert.AreEqual (1, GetAttributeCount (attributes,
697 typeof(DerivedTestCustomAttributeInherit)), "#A4");
699 attributes = Attribute.GetCustomAttributes (typeof(ClassB), true);
700 Assert.AreEqual (5, attributes.Length, "#B1");
701 Assert.AreEqual (1, GetAttributeCount (attributes,
702 typeof(TestCustomAttribute)), "#B2");
703 Assert.AreEqual (2, GetAttributeCount (attributes,
704 typeof(DerivedTestCustomAttributeMultiple)), "#B3");
705 Assert.AreEqual (1, GetAttributeCount (attributes,
706 typeof(DerivedTestCustomAttributeInherit)), "#B4");
707 Assert.AreEqual (1, GetAttributeCount (attributes,
708 typeof(DerivedTestCustomAttributeMultipleInherit)), "#B5");
712 public void TestEquality ()
714 MyCustomAttribute a = new MyCustomAttribute ("one");
715 MyCustomAttribute b = new MyCustomAttribute ("two");
716 MyCustomAttribute c = new MyCustomAttribute ("one");
717 MyCustomAttribute d = a;
719 Assert.IsTrue (a.Equals (c), "#1");
720 Assert.IsTrue (c.Equals (a), "#2");
721 Assert.IsFalse (c.Equals (b), "#3");
722 Assert.IsFalse (b.Equals (a), "#4");
723 Assert.IsFalse (b.Equals (c), "#5");
724 Assert.IsTrue (a.Equals (a), "#6");
725 Assert.IsTrue (a.Equals (d), "#7");
726 Assert.IsFalse (a.Equals (null), "#8");
730 class UserType : TypeDelegator {
731 public int GetCattr1;
732 public int GetCattr2;
734 public bool lastInherit;
735 public Type lastAttrType;
737 public UserType (Type type) : base (type) {}
739 public override object [] GetCustomAttributes (bool inherit)
742 lastInherit = inherit;
744 return base.GetCustomAttributes (inherit);
747 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
750 lastInherit = inherit;
751 lastAttrType = attributeType;
752 return base.GetCustomAttributes (attributeType, inherit);
755 public override bool IsDefined (Type attributeType, bool inherit)
758 lastInherit = inherit;
759 lastAttrType = attributeType;
760 return base.IsDefined (attributeType, inherit);
765 public void GetCustomAttributeOnUserType ()
767 UserType type = new UserType (typeof (AttributeTest));
768 var res = Attribute.GetCustomAttribute (type, typeof (TestFixtureAttribute));
769 Assert.IsNotNull (res, "#1");
770 Assert.AreEqual (typeof (TestFixtureAttribute), res.GetType (), "#2");
772 Assert.AreEqual (0, type.IsDef, "#4");
773 Assert.AreEqual (0, type.GetCattr1, "#5");
774 Assert.AreEqual (1, type.GetCattr2, "#6");
775 Assert.IsTrue (type.lastInherit, "#7");
776 Assert.AreEqual (typeof (TestFixtureAttribute), type.lastAttrType, "#8");
780 public void GetCustomAttributeOnMethodInfo ()
782 MemberInfo method = typeof (AttributeTest).GetMethod ("GetCustomAttributeOnMethodInfo");
783 var res = Attribute.GetCustomAttribute (method, typeof (TestAttribute));
785 Assert.IsNotNull (res, "#1");
786 Assert.AreEqual (typeof (TestAttribute), res.GetType (), "#2");
790 public void GetCustomAttributesOnUserType ()
792 UserType type = new UserType (typeof (AttributeTest));
793 var res = Attribute.GetCustomAttributes (type);
794 Assert.IsNotNull (res, "#1");
795 Assert.AreEqual (1, res.Length, "#2");
796 Assert.AreEqual (typeof (TestFixtureAttribute), res [0].GetType (), "#3");
798 Assert.AreEqual (0, type.IsDef, "#4");
799 Assert.AreEqual (0, type.GetCattr1, "#5");
800 Assert.AreEqual (1, type.GetCattr2, "#6");
801 Assert.IsTrue (type.lastInherit, "#7");
802 Assert.AreEqual (typeof (Attribute), type.lastAttrType, "#8");
806 public void IsDefinedOnUserType ()
808 UserType type = new UserType (typeof (AttributeTest));
809 var res = Attribute.IsDefined (type, typeof (TestFixtureAttribute));
810 Assert.IsTrue (res, "#1");
812 Assert.AreEqual (1, type.IsDef, "#4");
813 Assert.AreEqual (0, type.GetCattr1, "#5");
814 Assert.AreEqual (0, type.GetCattr2, "#6");
815 Assert.IsTrue (type.lastInherit, "#7");
816 Assert.AreEqual (typeof (TestFixtureAttribute), type.lastAttrType, "#8");
820 public void GetCustomAttributeOnNewSreTypes ()
822 AssemblyName assemblyName = new AssemblyName ();
823 assemblyName.Name = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
824 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
825 assemblyName, AssemblyBuilderAccess.Run);
826 ModuleBuilder module = assembly.DefineDynamicModule ("module1");
828 var tb = module.DefineType ("ns.type", TypeAttributes.Public);
829 var arr = tb.MakeArrayType ();
830 var ptr = tb.MakePointerType ();
831 var byref = tb.MakeByRefType ();
834 Attribute.GetCustomAttribute (arr, typeof (ObsoleteAttribute));
836 } catch (NotSupportedException) {}
839 Attribute.GetCustomAttribute (ptr, typeof (ObsoleteAttribute));
841 } catch (NotSupportedException) {}
844 Attribute.GetCustomAttribute (byref, typeof (ObsoleteAttribute));
846 } catch (NotSupportedException) {}
850 public void GetCustomAttributeOnBadSreTypes ()
852 AssemblyName assemblyName = new AssemblyName ();
853 assemblyName.Name = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
854 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
855 assemblyName, AssemblyBuilderAccess.Run);
856 ModuleBuilder module = assembly.DefineDynamicModule ("module1");
858 var tb = module.DefineType ("ns.type", TypeAttributes.Public);
859 tb.DefineGenericParameters ("T");
860 var ginst = tb.MakeGenericType (typeof (int));
862 Attribute.GetCustomAttribute (ginst, typeof (ObsoleteAttribute));
864 } catch (NotSupportedException) {}
869 private int GetAttributeCount (object[] attributes, Type attributeType)
873 foreach (Attribute attribute in attributes) {
874 if (attribute.GetType () == attributeType)
881 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
882 private class TestCustomAttribute : Attribute
886 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = false)]
887 private class DerivedTestCustomAttributeMultiple : TestCustomAttribute
891 [AttributeUsage (AttributeTargets.All, AllowMultiple = false, Inherited = true)]
892 private class DerivedTestCustomAttributeInherit : TestCustomAttribute
896 [AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = true)]
897 private class DerivedTestCustomAttributeMultipleInherit : TestCustomAttribute
901 [TestCustomAttribute]
902 [DerivedTestCustomAttributeMultiple]
903 [DerivedTestCustomAttributeInherit]
908 [TestCustomAttribute ()]
909 [DerivedTestCustomAttributeMultiple ()]
910 [DerivedTestCustomAttributeMultiple ()]
911 [DerivedTestCustomAttributeMultipleInherit ()]
912 private class ClassB : ClassA
916 [TestCustomAttribute ()]
917 [DerivedTestCustomAttributeMultiple ()]
918 [DerivedTestCustomAttributeMultipleInherit ()]
919 private class ClassC : ClassB
924 namespace ParamNamespace {
926 class FooAttribute : Attribute {}
927 class BarAttribute : Attribute {}
929 class DataAttribute : Attribute {
931 public string Data { get; set; }
933 public DataAttribute (string data)
941 public virtual void Bar ([Foo] string bar, [Data ("UltraBase.baz")] string baz)
946 class Base : UltraBase {
948 public override void Bar ([Data ("Base.bar")] string bar, string baz)
953 class Derived : Base {
955 public override void Bar ([Bar] string bar, [Data ("Derived.baz")] string baz)
962 public class ParamAttributeTest {
964 static ParameterInfo GetParameter (Type type, string method_name, string param_name)
966 foreach (var method in type.GetMethods ()) {
967 if (method.Name != method_name)
970 foreach (var parameter in method.GetParameters ())
971 if (parameter.Name == param_name)
979 public void IsDefinedTopLevel ()
981 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
983 Assert.IsNotNull (parameter);
984 Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.BarAttribute)));
988 public void IsDefinedHierarchy ()
990 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
992 Assert.IsNotNull (parameter);
993 Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.FooAttribute)));
997 public void IsDefinedHierarchyMultiple ()
999 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1001 Assert.IsNotNull (parameter);
1002 Assert.IsTrue (Attribute.IsDefined (parameter, typeof (ParamNamespace.DataAttribute)));
1006 public void GetCustomAttributeTopLevel ()
1008 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1010 Assert.IsNotNull (Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.BarAttribute)));
1014 public void GetCustomAttributeHierarchy ()
1016 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1017 var data = (ParamNamespace.DataAttribute) Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.DataAttribute));
1018 Assert.IsNotNull (data);
1019 Assert.AreEqual ("Base.bar", data.Data);
1023 public void GetCustomAttributeHierarchyMultiple ()
1025 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1026 var data = (ParamNamespace.DataAttribute) Attribute.GetCustomAttribute (parameter, typeof (ParamNamespace.DataAttribute));
1027 Assert.IsNotNull (data);
1028 Assert.AreEqual ("Derived.baz", data.Data);
1032 public void GetAllCustomAttributes ()
1034 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "bar");
1035 var attributes = (Attribute []) Attribute.GetCustomAttributes (parameter, true);
1036 Assert.AreEqual (3, attributes.Length);
1037 Assert.AreEqual (typeof (ParamNamespace.BarAttribute), attributes [0].GetType ());
1038 Assert.AreEqual (typeof (ParamNamespace.DataAttribute), attributes [1].GetType ());
1039 Assert.AreEqual (typeof (ParamNamespace.FooAttribute), attributes [2].GetType ());
1043 public void GetDataCustomAttributes ()
1045 var parameter = GetParameter (typeof (ParamNamespace.Derived), "Bar", "baz");
1046 var attributes = (ParamNamespace.DataAttribute []) Attribute.GetCustomAttributes (parameter, typeof (ParamNamespace.DataAttribute), true);
1047 Assert.AreEqual (1, attributes.Length);
1048 Assert.AreEqual ("Derived.baz", attributes [0].Data);