/
custom-attr.cs
67 lines (61 loc) · 1.6 KB
/
custom-attr.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Reflection;
namespace Test {
public class MyAttribute: Attribute {
public string val;
public MyAttribute (string stuff) {
System.Console.WriteLine (stuff);
val = stuff;
}
}
public class My2Attribute: MyAttribute {
public int ival;
public My2Attribute (string stuff, int blah) : base (stuff) {
System.Console.WriteLine ("ctor with int val"+stuff);
ival = blah;
}
}
public class My3Attribute : Attribute {
char[] array_val;
public char[] Prop {
get {
return array_val;
}
set {
array_val = value;
}
}
public char[] Prop2;
}
[My("testclass")]
[My2("testclass", 22)]
[My3(Prop = new char [] { 'A', 'B', 'C', 'D' }, Prop2 = new char [] { 'A', 'D' })]
public class Test {
static public int Main() {
System.Reflection.MemberInfo info = typeof (Test);
object[] attributes = info.GetCustomAttributes (false);
for (int i = 0; i < attributes.Length; i ++) {
System.Console.WriteLine(attributes[i]);
}
if (attributes.Length != 3)
return 1;
for (int i = 0; i < attributes.Length; ++i) {
if (attributes [i] is MyAttribute) {
if (((MyAttribute)attributes [i]).val != "testclass")
return 2;
}
if (attributes [i] is My3Attribute) {
if (new String (((My3Attribute)attributes [i]).Prop) != "ABCD") {
Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop));
return 3;
}
if (new String (((My3Attribute)attributes [i]).Prop2) != "AD") {
Console.WriteLine (new String (((My3Attribute)attributes [i]).Prop2));
return 4;
}
}
}
return 0;
}
}
}