Merge pull request #5714 from alexischr/update_bockbuild
[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         interface ZInterface {
44     }
45
46         class ZAttribute : Attribute, ZInterface {
47         }
48
49         [X, Z, Serializable]
50         class Y {
51         }
52
53         [My("arg\0string\0with\0nuls")]
54         class NulTests {
55         }
56                         
57         [My("testclass")]
58         [My2("testclass", 22)]
59         [My3(Prop = new char [] { 'A', 'B', 'C', 'D' }, Prop2 = new char [] { 'A', 'D' })]
60         public class Test {
61                 static public int Main() {
62                         System.Reflection.MemberInfo info = typeof (Test);
63                         object[] attributes = info.GetCustomAttributes (false);
64                         for (int i = 0; i < attributes.Length; i ++) {
65                                 System.Console.WriteLine(attributes[i]);
66                         }
67                         if (attributes.Length != 3)
68                                 return 1;
69                         for (int i = 0; i < attributes.Length; ++i) {
70                                 if (attributes [i] is MyAttribute) {
71                                         if (((MyAttribute)attributes [i]).val != "testclass")
72                                                 return 2;
73                                 }
74                                 if (attributes [i] is My3Attribute) {
75                                         if (new String (((My3Attribute)attributes [i]).Prop) != "ABCD") {
76                                                 Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop));
77                                                 return 3;
78                                         }
79                                         if (new String (((My3Attribute)attributes [i]).Prop2) != "AD") {
80                                                 Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop2));
81                                                 return 4;
82                                         }
83                                 }
84                         }
85
86                         //
87                         // Test that requesting a specific custom attributes does not
88                         // create all the others
89                         //
90
91                         typeof (Y).IsDefined (typeof (ZAttribute), true);
92                         typeof (Y).IsDefined (typeof (XAttribute), true);
93
94                         typeof (Y).GetCustomAttributes (typeof (ZAttribute), true);
95
96                         try {
97                                 typeof (Y).GetCustomAttributes (true);
98                                 return 4;
99                         }
100                         catch {
101                         }
102
103                         if (typeof (Y).GetCustomAttributes (typeof (ZInterface), true).Length != 1)
104                                 return 5;
105
106                         if (!typeof (Y).IsDefined (typeof (ZInterface), true))
107                                 return 6;
108
109                         // Test that synthetic methods have no attributes
110                         if (typeof(int[,]).GetConstructor (new Type [] { typeof (int), typeof (int) }).GetCustomAttributes (true).Length != 0)
111                                 return 7;
112
113                         // Test that nuls are preserved (see Xamarin bug 5732)
114                         if (((MyAttribute)typeof (NulTests).GetCustomAttributes (true)[0]).val != "arg\0string\0with\0nuls")
115                                 return 8;
116
117                         return 0;
118                 }
119         }
120 }