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