Revert "[2018-06] [System]: `MonoTlsStream` is now `IDisposable` and cleans up on...
[mono-project.git] / mono / tests / custom-attr.cs
blobb8d35607ae6dc3411731a31bead8c26b96274408
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("arg\0string\0with\0nuls")]
54 class NulTests {
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]);
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;
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;
79 if (new String (((My3Attribute)attributes [i]).Prop2) != "AD") {
80 Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop2));
81 return 4;
87 // Test that requesting a specific custom attributes does not
88 // create all the others
91 typeof (Y).IsDefined (typeof (ZAttribute), true);
92 typeof (Y).IsDefined (typeof (XAttribute), true);
94 typeof (Y).GetCustomAttributes (typeof (ZAttribute), true);
96 try {
97 typeof (Y).GetCustomAttributes (true);
98 return 4;
100 catch {
103 if (typeof (Y).GetCustomAttributes (typeof (ZInterface), true).Length != 1)
104 return 5;
106 if (!typeof (Y).IsDefined (typeof (ZInterface), true))
107 return 6;
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;
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;
117 return 0;