Add System.Net.Http to build
[mono-project.git] / mono / tests / custom-attr.cs
blob217a2ebaffe7117db1b2b6a9c8aceeaef82202db
2 using System;
3 using System.Reflection;
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;
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;
21 public class My3Attribute : Attribute {
22 char[] array_val;
24 public char[] Prop {
25 get {
26 return array_val;
28 set {
29 array_val = value;
33 public char[] Prop2;
36 class XAttribute : Attribute {
37 public XAttribute ()
39 throw new Exception ("X");
43 interface ZInterface {
46 class ZAttribute : Attribute, ZInterface {
49 [X, Z, Serializable]
50 class Y {
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]);
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;
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;
75 if (new String (((My3Attribute)attributes [i]).Prop2) != "AD") {
76 Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop2));
77 return 4;
83 // Test that requesting a specific custom attributes does not
84 // create all the others
87 typeof (Y).IsDefined (typeof (ZAttribute), true);
88 typeof (Y).IsDefined (typeof (XAttribute), true);
90 typeof (Y).GetCustomAttributes (typeof (ZAttribute), true);
92 try {
93 typeof (Y).GetCustomAttributes (true);
94 return 4;
96 catch {
99 if (typeof (Y).GetCustomAttributes (typeof (ZInterface), true).Length != 1)
100 return 5;
102 if (!typeof (Y).IsDefined (typeof (ZInterface), true))
103 return 6;
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;
109 return 0;