2003-04-11 Sebastien Pouliot <spouliot@videotron.ca>
[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 using System;
12 using System.Collections;
13 using System.Reflection;
14
15 namespace System.ComponentModel
16 {
17         public class AttributeCollection : ICollection, IEnumerable
18         {
19                 private ArrayList attrList = new ArrayList ();
20                 public static readonly AttributeCollection Empty = new AttributeCollection (null);
21                 
22                 public AttributeCollection (Attribute[] attributes)
23                 {
24                         if (attributes != null)
25                                 for (int i = 0; i < attributes.Length; i++)
26                                         attrList.Add (attributes[i]);
27                 }
28
29                 public bool Contains (Attribute attr)
30                 {
31                         return attrList.Contains (attr);
32                 }
33
34                 public bool Contains (Attribute [] attributes)
35                 {
36                         if (attributes == null)
37                                 return true;
38
39                         foreach (Attribute attr in attributes)
40                                 if (!Contains (attr))
41                                         return false;
42
43                         return true;
44                 }
45
46                 public void CopyTo (Array array, int index)
47                 {
48                         attrList.CopyTo (array, index);
49                 }
50
51                 public IEnumerator GetEnumerator ()
52                 {
53                         return attrList.GetEnumerator ();
54                 }
55
56                 public bool Matches (Attribute attr)
57                 {
58                         foreach (Attribute a in attrList)
59                                 if (a.Match (attr))
60                                         return true;
61                         return false;
62                 }
63
64                 public bool Matches (Attribute [] attributes)
65                 {
66                         foreach (Attribute a in attributes)
67                                 if (!(Matches (a)))
68                                         return false;
69                         return true;
70                 }
71
72                 protected Attribute GetDefaultAttribute (Type attributeType)
73                 {
74                         Attribute attr;
75                         BindingFlags bf = BindingFlags.Public | BindingFlags.Static;
76
77                         FieldInfo def = attributeType.GetField ("Default", bf);
78                         if (def == null) {
79                                 attr = Activator.CreateInstance (attributeType) as Attribute;
80                                 if (attr != null && !attr.IsDefaultAttribute ())
81                                         attr = null;
82                         } else {
83                                 attr = (Attribute) def.GetValue (null);
84                         }
85
86                         return attr;
87                 }
88
89                 public bool IsSynchronized {
90                         get {
91                                 return attrList.IsSynchronized;
92                         }
93                 }
94
95                 public object SyncRoot {
96                         get {
97                                 return attrList.SyncRoot;
98                         }
99                 }
100                 
101                 public int Count {
102                         get {
103                                 return attrList.Count;
104                         }
105                 }
106
107                 public virtual Attribute this[Type type] {
108                         get {
109                                 Attribute attr = null;
110                                 foreach (Attribute a in attrList) {
111                                         if (a.GetType () == type){
112                                                 attr = a;
113                                                 break;
114                                         }
115                                 }
116
117                                 if (attr == null)
118                                         attr = GetDefaultAttribute (type);
119
120                                 return attr;
121                         }
122                 }
123
124                 public virtual Attribute this[int index] {
125                         get {
126                                 return (Attribute) attrList [index];
127                         }
128                 }
129         }
130 }