Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / tests / test-397.cs
1 //
2 // Access modifiers
3 // We use reflection to test that the flags are the correct ones
4 //
5
6 using System;
7 using System.Reflection;
8
9  [AttributeUsage (AttributeTargets.Class)]
10  public class TypeCheckAttribute : Attribute {
11
12          public TypeCheckAttribute ()
13          {
14          }
15  }
16
17  [AttributeUsage (AttributeTargets.Property)]
18  public class PropertyCheckAttribute : Attribute {
19
20          public PropertyCheckAttribute ()
21          {
22          }
23  }
24
25  [AttributeUsage (AttributeTargets.Method)]
26  public class AccessorCheckAttribute : Attribute {
27          MethodAttributes flags;
28
29          public AccessorCheckAttribute (MethodAttributes flags)
30          {
31                  this.flags = flags;
32          }
33
34          public MethodAttributes Attributes {
35                  get {
36                          return flags;
37                  }
38          }
39  }
40
41  public class Test {
42
43          public static int Main (string [] args)
44          {
45                  Type t = typeof (A);
46                  
47                  foreach (PropertyInfo pi in t.GetProperties ()) {
48                          object [] attrs = pi.GetCustomAttributes (typeof (PropertyCheckAttribute), true);
49                          if (attrs == null)
50                                  return 0;
51                          
52                          MethodInfo get_accessor, set_accessor;
53                          get_accessor = pi.GetGetMethod (true);
54                          set_accessor = pi.GetSetMethod (true);
55                          
56                          if (get_accessor != null)
57                                  CheckFlags (pi, get_accessor);
58                          if (set_accessor != null)
59                                  CheckFlags (pi, set_accessor);
60                  }
61
62                  return 0;
63          }
64
65          static void CheckFlags (PropertyInfo pi, MethodInfo accessor)
66          {
67                  object [] attrs = accessor.GetCustomAttributes (typeof (AccessorCheckAttribute), true);
68                  if (attrs == null)
69                          return;
70
71                  AccessorCheckAttribute accessor_attr = (AccessorCheckAttribute) attrs [0];
72                  MethodAttributes accessor_flags = accessor.Attributes;
73
74                  if ((accessor_flags & accessor_attr.Attributes) == accessor_attr.Attributes)
75                          Console.WriteLine ("Test for {0}.{1} PASSED", pi.Name, accessor.Name);
76                  else {
77                          string message = String.Format ("Test for {0}.{1} INCORRECT: MethodAttributes should be {2}, but are {3}",
78                                          pi.Name, accessor.Name, accessor_attr.Attributes, accessor_flags);
79                          throw new Exception (message);
80                  }
81          }
82
83  }
84
85  [TypeCheck]
86  public class A {
87
88          const MethodAttributes flags = MethodAttributes.HideBySig |
89                  MethodAttributes.SpecialName;
90
91          [PropertyCheck]
92          public int Value1 {
93                  [AccessorCheck (flags | MethodAttributes.Public)]
94                  get {
95                          return 0;
96                  }
97                  [AccessorCheck (flags | MethodAttributes.Public)]
98                  set {
99                  }
100          }
101
102          [PropertyCheck]
103          public int Value2 {
104                  [AccessorCheck (flags | MethodAttributes.Public)]
105                  get {
106                          return 0;
107                  }
108                  [AccessorCheck (flags | MethodAttributes.FamORAssem)]
109                  protected internal set {
110                  }
111          }
112
113          [PropertyCheck]
114          public int Value3 {
115                  [AccessorCheck (flags | MethodAttributes.Public)]
116                  get {
117                          return 0;
118                  }
119                  [AccessorCheck (flags | MethodAttributes.Family)]
120                  protected set {
121                  }
122          }
123
124          [PropertyCheck]
125          public int Value4 {
126                  [AccessorCheck (flags | MethodAttributes.Assembly)]
127                  internal get {
128                          return 0;
129                  }
130                  [AccessorCheck (flags | MethodAttributes.Public)]
131                  set {
132                  }
133          }
134
135          [PropertyCheck]
136          public int Value5 {
137                  [AccessorCheck (flags | MethodAttributes.Public)]
138                  get {
139                          return 0;
140                  }
141                  [AccessorCheck (flags | MethodAttributes.Private)]
142                  private set {
143                  }
144          }
145
146  }
147