2 // System.ComponentModel.PropertyDescriptorCollection.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (C) Rodrigo Moya, 2002
10 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
11 // (C) 2003 Andreas Nahr
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 namespace System.ComponentModel
38 /// Represents a collection of PropertyDescriptor objects.
40 public class PropertyDescriptorCollection : IList, ICollection, IEnumerable, IDictionary
42 public static readonly PropertyDescriptorCollection Empty = new PropertyDescriptorCollection (null, true);
43 private ArrayList properties;
44 private bool readOnly;
46 public PropertyDescriptorCollection (PropertyDescriptor[] properties)
48 this.properties = new ArrayList ();
49 if (properties == null)
52 this.properties.AddRange (properties);
60 PropertyDescriptorCollection (PropertyDescriptor[] properties, bool readOnly) : this (properties)
62 this.readOnly = readOnly;
65 private PropertyDescriptorCollection ()
69 public int Add (PropertyDescriptor value)
72 throw new NotSupportedException ();
74 properties.Add (value);
75 return properties.Count - 1;
78 int IList.Add (object value)
80 return Add ((PropertyDescriptor) value);
83 void IDictionary.Add (object key, object value)
85 if ((value as PropertyDescriptor) == null) {
86 throw new ArgumentException ("value");
89 Add ((PropertyDescriptor) value);
95 throw new NotSupportedException ();
105 void IDictionary.Clear ()
110 public bool Contains (PropertyDescriptor value)
112 return properties.Contains (value);
115 bool IList.Contains (object value)
117 return Contains ((PropertyDescriptor) value);
120 bool IDictionary.Contains (object value)
122 return Contains ((PropertyDescriptor) value);
125 public void CopyTo (Array array, int index)
127 properties.CopyTo (array, index);
130 public virtual PropertyDescriptor Find (string name, bool ignoreCase)
133 throw new ArgumentNullException ("name");
136 foreach (PropertyDescriptor p in properties) {
137 if (0 == String.Compare (name, p.Name, ignoreCase))
143 public virtual IEnumerator GetEnumerator ()
145 return properties.GetEnumerator ();
149 IDictionaryEnumerator IDictionary.GetEnumerator ()
151 throw new NotImplementedException ();
154 public int IndexOf (PropertyDescriptor value)
156 return properties.IndexOf (value);
159 int IList.IndexOf (object value)
161 return IndexOf ((PropertyDescriptor) value);
164 public void Insert (int index, PropertyDescriptor value)
167 throw new NotSupportedException ();
169 properties.Insert (index, value);
172 void IList.Insert (int index, object value)
174 Insert (index, (PropertyDescriptor) value);
177 public void Remove (PropertyDescriptor value)
180 throw new NotSupportedException ();
182 properties.Remove (value);
185 void IDictionary.Remove (object value)
187 Remove ((PropertyDescriptor) value);
190 void IList.Remove (object value)
192 Remove ((PropertyDescriptor) value);
195 public void RemoveAt (int index)
198 throw new NotSupportedException ();
200 properties.RemoveAt (index);
203 void IList.RemoveAt (int index)
208 private PropertyDescriptorCollection CloneCollection ()
210 PropertyDescriptorCollection col = new PropertyDescriptorCollection ();
211 col.properties = (ArrayList) properties.Clone ();
215 public virtual PropertyDescriptorCollection Sort ()
217 PropertyDescriptorCollection col = CloneCollection ();
218 col.InternalSort ((IComparer) null);
222 public virtual PropertyDescriptorCollection Sort (IComparer comparer)
224 PropertyDescriptorCollection col = CloneCollection ();
225 col.InternalSort (comparer);
229 public virtual PropertyDescriptorCollection Sort (string[] order)
231 PropertyDescriptorCollection col = CloneCollection ();
232 col.InternalSort (order);
236 public virtual PropertyDescriptorCollection Sort (string[] order, IComparer comparer)
238 PropertyDescriptorCollection col = CloneCollection ();
239 ArrayList sorted = col.ExtractItems (order);
240 col.InternalSort (comparer);
241 sorted.AddRange (col.properties);
242 col.properties = sorted;
246 protected void InternalSort (IComparer ic)
248 properties.Sort (ic);
251 protected void InternalSort (string [] order)
253 ArrayList sorted = ExtractItems (order);
254 InternalSort ((IComparer) null);
255 sorted.AddRange (properties);
259 ArrayList ExtractItems (string[] names)
261 ArrayList sorted = new ArrayList (properties.Count);
262 object[] ext = new object [names.Length];
264 for (int n=0; n<properties.Count; n++)
266 PropertyDescriptor ed = (PropertyDescriptor) properties[n];
267 int i = Array.IndexOf (names, ed.Name);
270 properties.RemoveAt (n);
274 foreach (object ob in ext)
275 if (ob != null) sorted.Add (ob);
280 internal PropertyDescriptorCollection Filter (Attribute[] attributes)
282 ArrayList list = new ArrayList ();
283 foreach (PropertyDescriptor pd in properties) {
284 if (pd.Attributes.Contains (attributes)) {
288 PropertyDescriptor[] descriptors = new PropertyDescriptor[list.Count];
289 list.CopyTo (descriptors);
290 return new PropertyDescriptorCollection (descriptors, true);
293 bool IDictionary.IsFixedSize
304 bool IList.IsFixedSize
315 bool IList.IsReadOnly
322 bool IDictionary.IsReadOnly
330 bool ICollection.IsSynchronized
337 int ICollection.Count {
338 get { return Count; }
344 return properties.Count;
348 object ICollection.SyncRoot
355 ICollection IDictionary.Keys
358 string [] keys = new string [properties.Count];
360 foreach (PropertyDescriptor p in properties)
366 ICollection IDictionary.Values
369 return (ICollection) properties.Clone ();
373 object IDictionary.this [object key]
376 if (!(key is string))
378 return this [(string) key];
382 throw new NotSupportedException ();
385 if (!(key is string) || (value as PropertyDescriptor) == null)
386 throw new ArgumentException ();
387 int idx = properties.IndexOf (value);
389 Add ((PropertyDescriptor) value);
391 properties [idx] = value;
395 public virtual PropertyDescriptor this [string s]
398 return Find (s, false);
402 object IList.this [int index]
405 return properties [index];
409 throw new NotSupportedException ();
411 properties [index] = value;
415 public virtual PropertyDescriptor this [int index]
418 return (PropertyDescriptor) properties [index];