2 // acc-modifiers2.cs: We use reflection to test that the flags are the correct ones
6 using System.Reflection;
8 [AttributeUsage (AttributeTargets.Class)]
9 public class TypeCheckAttribute : Attribute {
11 public TypeCheckAttribute ()
16 [AttributeUsage (AttributeTargets.Property)]
17 public class PropertyCheckAttribute : Attribute {
19 public PropertyCheckAttribute ()
24 [AttributeUsage (AttributeTargets.Method)]
25 public class AccessorCheckAttribute : Attribute {
26 MethodAttributes flags;
28 public AccessorCheckAttribute (MethodAttributes flags)
33 public MethodAttributes Attributes {
42 public static int Main (string [] args)
46 foreach (PropertyInfo pi in t.GetProperties ()) {
47 object [] attrs = pi.GetCustomAttributes (typeof (PropertyCheckAttribute), true);
51 MethodInfo get_accessor, set_accessor;
52 get_accessor = pi.GetGetMethod (true);
53 set_accessor = pi.GetSetMethod (true);
55 if (get_accessor != null)
56 CheckFlags (pi, get_accessor);
57 if (set_accessor != null)
58 CheckFlags (pi, set_accessor);
64 static void CheckFlags (PropertyInfo pi, MethodInfo accessor)
66 object [] attrs = accessor.GetCustomAttributes (typeof (AccessorCheckAttribute), true);
70 AccessorCheckAttribute accessor_attr = (AccessorCheckAttribute) attrs [0];
71 MethodAttributes accessor_flags = accessor.Attributes;
73 if ((accessor_flags & accessor_attr.Attributes) == accessor_attr.Attributes)
74 Console.WriteLine ("Test for {0}.{1} PASSED", pi.Name, accessor.Name);
76 string message = String.Format ("Test for {0}.{1} INCORRECT: MethodAttributes should be {2}, but are {3}",
77 pi.Name, accessor.Name, accessor_attr.Attributes, accessor_flags);
78 throw new Exception (message);
87 const MethodAttributes flags = MethodAttributes.HideBySig |
88 MethodAttributes.SpecialName;
92 [AccessorCheck (flags | MethodAttributes.Public)]
96 [AccessorCheck (flags | MethodAttributes.Public)]
103 [AccessorCheck (flags | MethodAttributes.Public)]
107 [AccessorCheck (flags | MethodAttributes.FamORAssem)]
108 protected internal set {
114 [AccessorCheck (flags | MethodAttributes.Public)]
118 [AccessorCheck (flags | MethodAttributes.Family)]
125 [AccessorCheck (flags | MethodAttributes.Assembly)]
129 [AccessorCheck (flags | MethodAttributes.Public)]
136 [AccessorCheck (flags | MethodAttributes.Public)]
140 [AccessorCheck (flags | MethodAttributes.Private)]