Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[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("testclass")]
54         [My2("testclass", 22)]
55         [My3(Prop = new char [] { 'A', 'B', 'C', 'D' }, Prop2 = new char [] { 'A', 'D' })]
56         public class Test {
57                 static public int Main() {
58                         System.Reflection.MemberInfo info = typeof (Test);
59                         object[] attributes = info.GetCustomAttributes (false);
60                         for (int i = 0; i < attributes.Length; i ++) {
61                                 System.Console.WriteLine(attributes[i]);
62                         }
63                         if (attributes.Length != 3)
64                                 return 1;
65                         for (int i = 0; i < attributes.Length; ++i) {
66                                 if (attributes [i] is MyAttribute) {
67                                         if (((MyAttribute)attributes [i]).val != "testclass")
68                                                 return 2;
69                                 }
70                                 if (attributes [i] is My3Attribute) {
71                                         if (new String (((My3Attribute)attributes [i]).Prop) != "ABCD") {
72                                                 Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop));
73                                                 return 3;
74                                         }
75                                         if (new String (((My3Attribute)attributes [i]).Prop2) != "AD") {
76                                                 Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop2));
77                                                 return 4;
78                                         }
79                                 }
80                         }
81
82                         //
83                         // Test that requesting a specific custom attributes does not
84                         // create all the others
85                         //
86
87                         typeof (Y).IsDefined (typeof (ZAttribute), true);
88                         typeof (Y).IsDefined (typeof (XAttribute), true);
89
90                         typeof (Y).GetCustomAttributes (typeof (ZAttribute), true);
91
92                         try {
93                                 typeof (Y).GetCustomAttributes (true);
94                                 return 4;
95                         }
96                         catch {
97                         }
98
99                         if (typeof (Y).GetCustomAttributes (typeof (ZInterface), true).Length != 1)
100                                 return 5;
101
102                         if (!typeof (Y).IsDefined (typeof (ZInterface), true))
103                                 return 6;
104
105                         // Test that synthetic methods have no attributes
106                         if (typeof(int[,]).GetConstructor (new Type [] { typeof (int), typeof (int) }).GetCustomAttributes (true).Length != 0)
107                                 return 7;
108
109                         return 0;
110                 }
111         }
112 }