svn path=/branches/mono-1-1-9/mono/; revision=51217
[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         [My("testclass")]
37         [My2("testclass", 22)]
38         [My3(Prop = new char [] { 'A', 'B', 'C', 'D' }, Prop2 = new char [] { 'A', 'D' })]
39         public class Test {
40                 static public int Main() {
41                         System.Reflection.MemberInfo info = typeof (Test);
42                         object[] attributes = info.GetCustomAttributes (false);
43                         for (int i = 0; i < attributes.Length; i ++) {
44                                 System.Console.WriteLine(attributes[i]);
45                         }
46                         if (attributes.Length != 3)
47                                 return 1;
48                         for (int i = 0; i < attributes.Length; ++i) {
49                                 if (attributes [i] is MyAttribute) {
50                                         if (((MyAttribute)attributes [i]).val != "testclass")
51                                                 return 2;
52                                 }
53                                 if (attributes [i] is My3Attribute) {
54                                         if (new String (((My3Attribute)attributes [i]).Prop) != "ABCD") {
55                                                 Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop));
56                                                 return 3;
57                                         }
58                                         if (new String (((My3Attribute)attributes [i]).Prop2) != "AD") {
59                                                 Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop2));
60                                                 return 4;
61                                         }
62                                 }
63                         }
64                         return 0;
65                 }
66         }
67 }