2005-12-22 John Luke <john.luke@gmail.com>
[mono-project.git] / mcs / class / System / System.ComponentModel / AttributeCollection.cs
blob658117ac1af0982a8042209ee40985fa39bad2e7
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 //
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.
32 using System;
33 using System.Collections;
34 using System.Reflection;
35 using System.Runtime.InteropServices;
37 namespace System.ComponentModel
39 [ComVisible (true)]
40 public class AttributeCollection : ICollection, IEnumerable
42 private ArrayList attrList = new ArrayList ();
43 public static readonly AttributeCollection Empty = new AttributeCollection ((ArrayList)null);
45 internal AttributeCollection (ArrayList attributes)
47 attrList = attributes;
50 public AttributeCollection (Attribute[] attributes)
52 if (attributes != null)
53 for (int i = 0; i < attributes.Length; i++)
54 attrList.Add (attributes[i]);
57 public bool Contains (Attribute attr)
59 Attribute at = this [attr.GetType ()];
60 if (at != null)
61 return attr.Equals (at);
62 else
63 return false;
66 public bool Contains (Attribute [] attributes)
68 if (attributes == null)
69 return true;
71 foreach (Attribute attr in attributes)
72 if (!Contains (attr))
73 return false;
75 return true;
78 public void CopyTo (Array array, int index)
80 attrList.CopyTo (array, index);
83 public IEnumerator GetEnumerator ()
85 return attrList.GetEnumerator ();
88 public bool Matches (Attribute attr)
90 foreach (Attribute a in attrList)
91 if (a.Match (attr))
92 return true;
93 return false;
96 public bool Matches (Attribute [] attributes)
98 foreach (Attribute a in attributes)
99 if (!(Matches (a)))
100 return false;
101 return true;
104 protected Attribute GetDefaultAttribute (Type attributeType)
106 Attribute attr = null;
107 BindingFlags bf = BindingFlags.Public | BindingFlags.Static;
109 FieldInfo def = attributeType.GetField ("Default", bf);
110 if (def == null) {
111 ConstructorInfo constructorInfo = attributeType.GetConstructor (Type.EmptyTypes);
112 if (constructorInfo != null)
113 attr = constructorInfo.Invoke (null) as Attribute;
114 if (attr != null && !attr.IsDefaultAttribute ())
115 attr = null;
116 } else {
117 attr = (Attribute) def.GetValue (null);
120 return attr;
123 bool ICollection.IsSynchronized {
124 get {
125 return attrList.IsSynchronized;
129 object ICollection.SyncRoot {
130 get {
131 return attrList.SyncRoot;
135 public int Count {
136 get {
137 return attrList.Count;
141 public virtual Attribute this[Type type] {
142 get {
143 Attribute attr = null;
144 foreach (Attribute a in attrList) {
145 if (type.IsAssignableFrom (a.GetType ())) {
146 attr = a;
147 break;
151 if (attr == null)
152 attr = GetDefaultAttribute (type);
154 return attr;
158 public virtual Attribute this[int index] {
159 get {
160 return (Attribute) attrList [index];