fixes to make it compile again
[mono.git] / mcs / class / corlib / System / Attribute.cs
1 //\r
2 // System.Attribute.cs\r
3 //\r
4 // Authors:\r
5 //   Miguel de Icaza (miguel@ximian.com) - Original\r
6 //   Nick D. Drochak II (ndrochak@gol.com) - Implemented most of the guts\r
7 //\r
8 // (C) Ximian, Inc.  http://www.ximian.com\r
9 //\r
10 \r
11 using System;\r
12 namespace System {\r
13 \r
14         public abstract class Attribute {\r
15 \r
16                 protected Attribute ()\r
17                 {\r
18                 }\r
19 \r
20                 public virtual object TypeId {\r
21                         get {\r
22                                 // Derived classes should override this default behaviour as appropriate\r
23                                 return this.GetType();\r
24                         }\r
25                 }\r
26 \r
27                 private static void CheckParameters(object element, Type attributeType)\r
28                 {\r
29                         // neither parameter is allowed to be null\r
30                         if (null == element) \r
31                         {\r
32                                 throw new ArgumentNullException("element");\r
33                         }\r
34 \r
35                         if (null == attributeType) \r
36                         {\r
37                                 throw new ArgumentNullException("attributeType");\r
38                         }\r
39 \r
40                 }\r
41 \r
42                 private static System.Attribute FindAttribute(object[] attributes)\r
43                 {\r
44                         // if there exists more than one attribute of the given type, throw an exception\r
45                         if (attributes.Length > 1) \r
46                         {\r
47                                 throw new System.Reflection.AmbiguousMatchException("<element> has more than one attribute of type <attributeType>");\r
48                         }\r
49 \r
50                         if (attributes.Length < 1) \r
51                         {\r
52                                 return (System.Attribute) null;\r
53                         }\r
54 \r
55                         // tested above for '> 1' and and '< 1', so only '== 1' is left, i.e. we found the attribute\r
56                         return (System.Attribute) attributes[0];\r
57                 }\r
58 \r
59                 private static void CheckAncestry(Type attributeType)\r
60                 {\r
61                         // attributeType must be derived from type System.Attribute\r
62                         Type t = typeof(System.Attribute);\r
63                         \r
64                         /* fixme: thgi does not work for target monolib2\r
65                         if (!attributeType.IsSubclassOf(t))\r
66                         {\r
67                                 throw new ArgumentException("Parameter is not a type derived from System.Attribute", "attributeType");\r
68                         }\r
69                         */\r
70                 }\r
71 \r
72                 public static Attribute GetCustomAttribute(System.Reflection.ParameterInfo element, Type attributeType){\r
73                         return GetCustomAttribute(element, attributeType, true);\r
74                 }\r
75 \r
76                 public static Attribute GetCustomAttribute(System.Reflection.MemberInfo element, Type attributeType){\r
77                         return GetCustomAttribute(element, attributeType, true);\r
78                 }\r
79 \r
80                 public static Attribute GetCustomAttribute(System.Reflection.Assembly element, Type attributeType){                     return GetCustomAttribute(element, attributeType, true);\r
81                 }\r
82 \r
83                 public static Attribute GetCustomAttribute(System.Reflection.Module element, Type attributeType){\r
84                         return GetCustomAttribute(element, attributeType, true);\r
85                 }\r
86 \r
87                 public static Attribute GetCustomAttribute(System.Reflection.Module element, Type attributeType, bool inherit){\r
88                         // neither parameter is allowed to be null\r
89                         CheckParameters(element, attributeType);\r
90 \r
91                         // attributeType must be derived from type System.Attribute\r
92                         CheckAncestry(attributeType);\r
93 \r
94                         // Module inheritance hierarchies CAN NOT be searched for attributes, so the second\r
95                         // parameter of GetCustomAttributes() is INGNORED.\r
96                         object[] attributes = element.GetCustomAttributes(attributeType, inherit);\r
97 \r
98                         return FindAttribute(attributes);\r
99                 }\r
100 \r
101                 public static Attribute GetCustomAttribute(System.Reflection.Assembly element, Type attributeType, bool inherit){\r
102                         // neither parameter is allowed to be null\r
103                         CheckParameters(element, attributeType);\r
104 \r
105                         // attributeType must be derived from type System.Attribute\r
106                         CheckAncestry(attributeType);\r
107 \r
108                         // Assembly inheritance hierarchies CAN NOT be searched for attributes, so the second\r
109                         // parameter of GetCustomAttributes() is INGNORED.\r
110                         object[] attributes = element.GetCustomAttributes(attributeType, inherit);\r
111 \r
112                         return FindAttribute(attributes);\r
113                 }\r
114                 public static Attribute GetCustomAttribute(System.Reflection.ParameterInfo element, Type attributeType, bool inherit){\r
115                         // neither parameter is allowed to be null\r
116                         CheckParameters(element, attributeType);\r
117 \r
118                         // attributeType must be derived from type System.Attribute\r
119                         CheckAncestry(attributeType);\r
120 \r
121                         // ParameterInfo inheritance hierarchies CAN NOT be searched for attributes, so the second\r
122                         // parameter of GetCustomAttributes() is INGNORED.\r
123                         object[] attributes = element.GetCustomAttributes(attributeType, inherit);\r
124 \r
125                         return FindAttribute(attributes);\r
126                 }\r
127                 public static Attribute GetCustomAttribute(System.Reflection.MemberInfo element, Type attributeType, bool inherit){\r
128                         // neither parameter is allowed to be null\r
129                         CheckParameters(element, attributeType);\r
130 \r
131                         // attributeType must be derived from type System.Attribute\r
132                         CheckAncestry(attributeType);\r
133 \r
134                         // MemberInfo inheritance hierarchies can be searched for attributes, so the second\r
135                         // parameter of GetCustomAttributes() is respected.\r
136                         object[] attributes = element.GetCustomAttributes(attributeType, inherit);\r
137 \r
138                         return FindAttribute(attributes);\r
139                 }\r
140 \r
141                 public static Attribute[] GetCustomAttributes(System.Reflection.Assembly element){\r
142                         return System.Attribute.GetCustomAttributes(element, true);\r
143                 }\r
144                 public static Attribute[] GetCustomAttributes(System.Reflection.ParameterInfo element){\r
145                         return System.Attribute.GetCustomAttributes(element, true);\r
146                 }\r
147                 public static Attribute[] GetCustomAttributes(System.Reflection.MemberInfo element){\r
148                         return System.Attribute.GetCustomAttributes(element, true);\r
149                 }\r
150                 public static Attribute[] GetCustomAttributes(System.Reflection.Module element){\r
151                         return System.Attribute.GetCustomAttributes(element, true);\r
152                 }\r
153                 public static Attribute[] GetCustomAttributes(System.Reflection.Assembly element, Type attributeType){\r
154                         return System.Attribute.GetCustomAttributes(element, attributeType, true);\r
155                 }\r
156                 public static Attribute[] GetCustomAttributes(System.Reflection.Module element, Type attributeType){\r
157                         return System.Attribute.GetCustomAttributes(element, attributeType, true);\r
158                 }\r
159                 public static Attribute[] GetCustomAttributes(System.Reflection.ParameterInfo element, Type attributeType){\r
160                         return System.Attribute.GetCustomAttributes(element, attributeType, true);\r
161                 }\r
162                 public static Attribute[] GetCustomAttributes(System.Reflection.MemberInfo element, Type attributeType)\r
163                 {\r
164                         return System.Attribute.GetCustomAttributes(element, attributeType, true);\r
165                 }\r
166                 public static Attribute[] GetCustomAttributes(System.Reflection.Assembly element, Type attributeType, bool inherit)\r
167                 {\r
168                         // element parameter is not allowed to be null\r
169                         CheckParameters(element, attributeType);\r
170 \r
171                         // make a properly typed array to return containing the custom attributes\r
172                         System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(attributeType, inherit);\r
173 \r
174                         return attributes;\r
175                 }\r
176                 public static Attribute[] GetCustomAttributes(System.Reflection.ParameterInfo element, Type attributeType, bool inherit){\r
177                         // element parameter is not allowed to be null\r
178                         CheckParameters(element, attributeType);\r
179 \r
180                         // make a properly typed array to return containing the custom attributes\r
181                         System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(attributeType, inherit);\r
182 \r
183                         return attributes;\r
184                 }\r
185                 public static Attribute[] GetCustomAttributes(System.Reflection.Module element, Type attributeType, bool inherit){\r
186                         // element parameter is not allowed to be null\r
187                         CheckParameters(element, attributeType);\r
188 \r
189                         // make a properly typed array to return containing the custom attributes\r
190                         System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(attributeType, inherit);\r
191 \r
192                         return attributes;\r
193                 }\r
194                 public static Attribute[] GetCustomAttributes(System.Reflection.MemberInfo element, Type attributeType, bool inherit)\r
195                 {\r
196                         // element parameter is not allowed to be null\r
197                         CheckParameters(element, attributeType);\r
198 \r
199                         // make a properly typed array to return containing the custom attributes\r
200                         System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(attributeType, inherit);\r
201 \r
202                         return attributes;\r
203                 }\r
204                 public static Attribute[] GetCustomAttributes(System.Reflection.Module element, bool inherit)\r
205                 {\r
206                         // element parameter is not allowed to be null\r
207                         CheckParameters(element, typeof(System.Attribute));\r
208 \r
209                         // make a properly typed array to return containing the custom attributes\r
210                         System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(inherit);\r
211 \r
212                         return attributes;\r
213                 }\r
214                 public static Attribute[] GetCustomAttributes(System.Reflection.Assembly element, bool inherit){\r
215                         // element parameter is not allowed to be null\r
216                         CheckParameters(element, typeof(System.Attribute));\r
217 \r
218                         // make a properly typed array to return containing the custom attributes\r
219                         System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(inherit);\r
220 \r
221                         return attributes;\r
222                 }\r
223                 public static Attribute[] GetCustomAttributes(System.Reflection.MemberInfo element, bool inherit){\r
224                         // element parameter is not allowed to be null\r
225                         CheckParameters(element, typeof(System.Attribute));\r
226 \r
227                         // make a properly typed array to return containing the custom attributes\r
228                         System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(inherit);\r
229 \r
230                         return attributes;\r
231                 }\r
232                 public static Attribute[] GetCustomAttributes(System.Reflection.ParameterInfo element, bool inherit){\r
233                         // element parameter is not allowed to be null\r
234                         CheckParameters(element, typeof(System.Attribute));\r
235 \r
236                         // make a properly typed array to return containing the custom attributes\r
237                         System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(inherit);\r
238 \r
239                         return attributes;\r
240                 }\r
241 \r
242                 public override int GetHashCode(){\r
243                         // TODO: Implement me\r
244                         return 0;\r
245                 }\r
246                 \r
247                 public virtual bool IsDefaultAttribute(){\r
248                         // Derived classes should override this default behaviour as appropriate\r
249                         return false;\r
250                 }\r
251                 \r
252                 public static bool IsDefined(System.Reflection.Module element, Type attributeType){\r
253                         return (System.Attribute.GetCustomAttributes(element, attributeType).Length > 0);\r
254                 }\r
255                 public static bool IsDefined(System.Reflection.ParameterInfo element, Type attributeType){\r
256                         return (System.Attribute.GetCustomAttributes(element, attributeType).Length > 0);\r
257                 }\r
258                 public static bool IsDefined(System.Reflection.MemberInfo element, Type attributeType){\r
259                         return (System.Attribute.GetCustomAttributes(element, attributeType).Length > 0);\r
260                 }\r
261                 public static bool IsDefined(System.Reflection.Assembly element, Type attributeType){\r
262                         return (System.Attribute.GetCustomAttributes(element, attributeType).Length > 0);\r
263                 }\r
264                 public static bool IsDefined(System.Reflection.MemberInfo element, Type attributeType, bool inherit){\r
265                         return (System.Attribute.GetCustomAttributes(element, attributeType, inherit).Length > 0);\r
266                 }\r
267                 public static bool IsDefined(System.Reflection.Assembly element, Type attributeType, bool inherit){\r
268                         return (System.Attribute.GetCustomAttributes(element, attributeType, inherit).Length > 0);\r
269                 }\r
270                 public static bool IsDefined(System.Reflection.Module element, Type attributeType, bool inherit){\r
271                         return (System.Attribute.GetCustomAttributes(element, attributeType, inherit).Length > 0);\r
272                 }\r
273                 public static bool IsDefined(System.Reflection.ParameterInfo element, Type attributeType, bool inherit){\r
274                         return (System.Attribute.GetCustomAttributes(element, attributeType, inherit).Length > 0);\r
275                 }\r
276                 \r
277                 public virtual bool Match(object obj){\r
278                         // default action is the same as Equals.  Derived classes should override as appropriate\r
279                         return this.Equals(obj);\r
280                 }\r
281 \r
282         }\r
283 \r
284 }\r