merge -r 53370:58178
[mono.git] / mono / tests / custom-attr.cs
1
2 using System;
3 using System.Reflection;
4
5 namespace Test {
6         public class MyAttribute: Attribute {
7                 public string val;
8                 public MyAttribute (string stuff) {
9                         System.Console.WriteLine (stuff);
10                         val = stuff;
11                 }
12         }
13         public class My2Attribute: MyAttribute {
14                 public int ival;
15                 public My2Attribute (string stuff, int blah) : base (stuff) {
16                         System.Console.WriteLine ("ctor with int val"+stuff);
17                         ival = blah;
18                 }
19         }
20
21         public class My3Attribute : Attribute {
22                 char[] array_val;
23
24                 public char[] Prop {
25                         get {
26                                 return array_val;
27                         }
28                         set {
29                                 array_val = value;
30                         }
31                 }
32
33                 public char[] Prop2;
34         }
35
36         class XAttribute : Attribute {
37                 public XAttribute () 
38                 {
39                         throw new Exception ("X");
40                 }
41         }
42
43         class ZAttribute : Attribute {
44         }
45
46         [X, Z, Serializable]
47         class Y {
48         }
49                         
50         [My("testclass")]
51         [My2("testclass", 22)]
52         [My3(Prop = new char [] { 'A', 'B', 'C', 'D' }, Prop2 = new char [] { 'A', 'D' })]
53         public class Test {
54                 static public int Main() {
55                         System.Reflection.MemberInfo info = typeof (Test);
56                         object[] attributes = info.GetCustomAttributes (false);
57                         for (int i = 0; i < attributes.Length; i ++) {
58                                 System.Console.WriteLine(attributes[i]);
59                         }
60                         if (attributes.Length != 3)
61                                 return 1;
62                         for (int i = 0; i < attributes.Length; ++i) {
63                                 if (attributes [i] is MyAttribute) {
64                                         if (((MyAttribute)attributes [i]).val != "testclass")
65                                                 return 2;
66                                 }
67                                 if (attributes [i] is My3Attribute) {
68                                         if (new String (((My3Attribute)attributes [i]).Prop) != "ABCD") {
69                                                 Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop));
70                                                 return 3;
71                                         }
72                                         if (new String (((My3Attribute)attributes [i]).Prop2) != "AD") {
73                                                 Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop2));
74                                                 return 4;
75                                         }
76                                 }
77                         }
78
79                         //
80                         // Test that requesting a specific custom attributes does not
81                         // create all the others
82                         //
83
84                         typeof (Y).IsDefined (typeof (ZAttribute), true);
85                         typeof (Y).IsDefined (typeof (XAttribute), true);
86
87                         typeof (Y).GetCustomAttributes (typeof (ZAttribute), true);
88
89                         try {
90                                 typeof (Y).GetCustomAttributes (true);
91                                 return 4;
92                         }
93                         catch {
94                         }
95
96                         return 0;
97                 }
98         }
99 }