2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System / System.ComponentModel / AttributeCollection.cs
1 //
2 // System.ComponentModel.AttributeCollection.cs
3 //
4 // Authors:
5 //      Rodrigo Moya (rodrigo@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34 using System.Reflection;
35 using System.Runtime.InteropServices;
36
37 namespace System.ComponentModel
38 {
39         [ComVisible (true)]
40         public class AttributeCollection : ICollection
41         {
42                 private ArrayList attrList = new ArrayList ();
43                 public static readonly AttributeCollection Empty = new AttributeCollection ((ArrayList)null);
44                 
45                 internal AttributeCollection (ArrayList attributes)
46                 {
47                         if (attributes != null)
48                                 attrList = attributes;
49                 }
50                 
51 #if NET_2_0
52                 public AttributeCollection (params Attribute[] attributes)
53 #else
54                 public AttributeCollection (Attribute[] attributes)
55 #endif
56                 {
57                         if (attributes != null)
58                                 for (int i = 0; i < attributes.Length; i++)
59                                         attrList.Add (attributes[i]);
60                 }
61
62 #if NET_2_0
63                 public static AttributeCollection FromExisting (AttributeCollection existing, params Attribute [] newAttributes)
64                 {
65                         if (existing == null)
66                                 throw new ArgumentNullException ("existing");
67                         AttributeCollection ret = new AttributeCollection ();
68                         ret.attrList.AddRange (existing.attrList);
69                         if (newAttributes != null)
70                                 ret.attrList.AddRange (newAttributes);
71                         return ret;
72                 }
73 #endif
74
75                 public bool Contains (Attribute attr)
76                 {
77                         Attribute at = this [attr.GetType ()];
78                         if (at != null)
79                                 return attr.Equals (at);
80                         else
81                                 return false;
82                 }
83
84                 public bool Contains (Attribute [] attributes)
85                 {
86                         if (attributes == null)
87                                 return true;
88
89                         foreach (Attribute attr in attributes)
90                                 if (!Contains (attr))
91                                         return false;
92
93                         return true;
94                 }
95
96                 public void CopyTo (Array array, int index)
97                 {
98                         attrList.CopyTo (array, index);
99                 }
100
101                 IEnumerator IEnumerable.GetEnumerator () {
102                         return GetEnumerator ();
103                 }
104
105                 public IEnumerator GetEnumerator ()
106                 {
107                         return attrList.GetEnumerator ();
108                 }
109
110                 public bool Matches (Attribute attr)
111                 {
112                         foreach (Attribute a in attrList)
113                                 if (a.Match (attr))
114                                         return true;
115                         return false;
116                 }
117
118                 public bool Matches (Attribute [] attributes)
119                 {
120                         foreach (Attribute a in attributes)
121                                 if (!(Matches (a)))
122                                         return false;
123                         return true;
124                 }
125
126                 protected Attribute GetDefaultAttribute (Type attributeType)
127                 {
128                         Attribute attr = null;
129                         BindingFlags bf = BindingFlags.Public | BindingFlags.Static;
130
131                         FieldInfo def = attributeType.GetField ("Default", bf);
132                         if (def == null) {
133                                 ConstructorInfo constructorInfo = attributeType.GetConstructor (Type.EmptyTypes);
134                                 if (constructorInfo != null)
135                                         attr = constructorInfo.Invoke (null) as Attribute;
136                                 if (attr != null && !attr.IsDefaultAttribute ())
137                                         attr = null;
138                         } else {
139                                 attr = (Attribute) def.GetValue (null);
140                         }
141
142                         return attr;
143                 }
144
145                 bool ICollection.IsSynchronized {
146                         get {
147                                 return attrList.IsSynchronized;
148                         }
149                 }
150
151                 object ICollection.SyncRoot {
152                         get {
153                                 return attrList.SyncRoot;
154                         }
155                 }
156                 
157                 int ICollection.Count {
158                         get {
159                                 return Count;
160                         }
161                 }
162
163                 public int Count {
164                         get {
165                                 return attrList != null ? attrList.Count : 0;
166                         }
167                 }
168
169                 public virtual Attribute this[Type type] {
170                         get {
171                                 Attribute attr = null;
172                                 if (attrList != null) {
173                                         foreach (Attribute a in attrList) {
174                                                 if (type.IsAssignableFrom (a.GetType ())) {
175                                                         attr = a;
176                                                         break;
177                                                 }
178                                         }
179                                 }
180                                 
181                                 if (attr == null)
182                                         attr = GetDefaultAttribute (type);
183
184                                 return attr;
185                         }
186                 }
187
188                 public virtual Attribute this[int index] {
189                         get {
190                                 return (Attribute) attrList [index];
191                         }
192                 }
193         }
194 }