New test.
[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                         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                         Attribute at = this [attr.GetType ()];
60                         if (at != null)
61                                 return attr.Equals (at);
62                         else
63                                 return false;
64                 }
65
66                 public bool Contains (Attribute [] attributes)
67                 {
68                         if (attributes == null)
69                                 return true;
70
71                         foreach (Attribute attr in attributes)
72                                 if (!Contains (attr))
73                                         return false;
74
75                         return true;
76                 }
77
78                 public void CopyTo (Array array, int index)
79                 {
80                         attrList.CopyTo (array, index);
81                 }
82
83                 IEnumerator IEnumerable.GetEnumerator () {
84                         return GetEnumerator ();
85                 }
86
87                 public IEnumerator GetEnumerator ()
88                 {
89                         return attrList.GetEnumerator ();
90                 }
91
92                 public bool Matches (Attribute attr)
93                 {
94                         foreach (Attribute a in attrList)
95                                 if (a.Match (attr))
96                                         return true;
97                         return false;
98                 }
99
100                 public bool Matches (Attribute [] attributes)
101                 {
102                         foreach (Attribute a in attributes)
103                                 if (!(Matches (a)))
104                                         return false;
105                         return true;
106                 }
107
108                 protected Attribute GetDefaultAttribute (Type attributeType)
109                 {
110                         Attribute attr = null;
111                         BindingFlags bf = BindingFlags.Public | BindingFlags.Static;
112
113                         FieldInfo def = attributeType.GetField ("Default", bf);
114                         if (def == null) {
115                                 ConstructorInfo constructorInfo = attributeType.GetConstructor (Type.EmptyTypes);
116                                 if (constructorInfo != null)
117                                         attr = constructorInfo.Invoke (null) as Attribute;
118                                 if (attr != null && !attr.IsDefaultAttribute ())
119                                         attr = null;
120                         } else {
121                                 attr = (Attribute) def.GetValue (null);
122                         }
123
124                         return attr;
125                 }
126
127                 bool ICollection.IsSynchronized {
128                         get {
129                                 return attrList.IsSynchronized;
130                         }
131                 }
132
133                 object ICollection.SyncRoot {
134                         get {
135                                 return attrList.SyncRoot;
136                         }
137                 }
138                 
139                 int ICollection.Count {
140                         get {
141                                 return Count;
142                         }
143                 }
144
145                 public int Count {
146                         get {
147                                 return attrList.Count;
148                         }
149                 }
150
151                 public virtual Attribute this[Type type] {
152                         get {
153                                 Attribute attr = null;
154                                 foreach (Attribute a in attrList) {
155                                         if (type.IsAssignableFrom (a.GetType ())) {
156                                                 attr = a;
157                                                 break;
158                                         }
159                                 }
160
161                                 if (attr == null)
162                                         attr = GetDefaultAttribute (type);
163
164                                 return attr;
165                         }
166                 }
167
168                 public virtual Attribute this[int index] {
169                         get {
170                                 return (Attribute) attrList [index];
171                         }
172                 }
173         }
174 }