2004-10-01 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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, IEnumerable
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                         attrList = attributes;
48                 }
49                 
50                 public AttributeCollection (Attribute[] attributes)
51                 {
52                         if (attributes != null)
53                                 for (int i = 0; i < attributes.Length; i++)
54                                         attrList.Add (attributes[i]);
55                 }
56
57                 public bool Contains (Attribute attr)
58                 {
59                         return attrList.Contains (attr);
60                 }
61
62                 public bool Contains (Attribute [] attributes)
63                 {
64                         if (attributes == null)
65                                 return true;
66
67                         foreach (Attribute attr in attributes)
68                                 if (!Contains (attr))
69                                         return false;
70
71                         return true;
72                 }
73
74                 public void CopyTo (Array array, int index)
75                 {
76                         attrList.CopyTo (array, index);
77                 }
78
79                 public IEnumerator GetEnumerator ()
80                 {
81                         return attrList.GetEnumerator ();
82                 }
83
84                 public bool Matches (Attribute attr)
85                 {
86                         foreach (Attribute a in attrList)
87                                 if (a.Match (attr))
88                                         return true;
89                         return false;
90                 }
91
92                 public bool Matches (Attribute [] attributes)
93                 {
94                         foreach (Attribute a in attributes)
95                                 if (!(Matches (a)))
96                                         return false;
97                         return true;
98                 }
99
100                 protected Attribute GetDefaultAttribute (Type attributeType)
101                 {
102                         Attribute attr = null;
103                         BindingFlags bf = BindingFlags.Public | BindingFlags.Static;
104
105                         FieldInfo def = attributeType.GetField ("Default", bf);
106                         if (def == null) {
107                                 ConstructorInfo constructorInfo = attributeType.GetConstructor (Type.EmptyTypes);
108                                 if (constructorInfo != null)
109                                         attr = constructorInfo.Invoke (null) as Attribute;
110                                 if (attr != null && !attr.IsDefaultAttribute ())
111                                         attr = null;
112                         } else {
113                                 attr = (Attribute) def.GetValue (null);
114                         }
115
116                         return attr;
117                 }
118
119                 bool ICollection.IsSynchronized {
120                         get {
121                                 return attrList.IsSynchronized;
122                         }
123                 }
124
125                 object ICollection.SyncRoot {
126                         get {
127                                 return attrList.SyncRoot;
128                         }
129                 }
130                 
131                 public int Count {
132                         get {
133                                 return attrList.Count;
134                         }
135                 }
136
137                 public virtual Attribute this[Type type] {
138                         get {
139                                 Attribute attr = null;
140                                 foreach (Attribute a in attrList) {
141                                         if (type.IsAssignableFrom (a.GetType ())) {
142                                                 attr = a;
143                                                 break;
144                                         }
145                                 }
146
147                                 if (attr == null)
148                                         attr = GetDefaultAttribute (type);
149
150                                 return attr;
151                         }
152                 }
153
154                 public virtual Attribute this[int index] {
155                         get {
156                                 return (Attribute) attrList [index];
157                         }
158                 }
159         }
160 }