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 //[DefaultMember ("Item")]
41 public class PropertyDescriptorCollection
: IList
, ICollection
, IEnumerable
, IDictionary
43 public static readonly PropertyDescriptorCollection Empty
= new PropertyDescriptorCollection ((ArrayList
)null);
46 internal PropertyDescriptorCollection (ArrayList list
)
51 public PropertyDescriptorCollection (PropertyDescriptor
[] properties
)
53 this.properties
= new ArrayList ();
54 if (properties
== null)
57 this.properties
.AddRange (properties
);
60 private PropertyDescriptorCollection ()
64 public int Add (PropertyDescriptor
value)
66 properties
.Add (value);
67 return properties
.Count
- 1;
70 int IList
.Add (object value)
72 return Add ((PropertyDescriptor
) value);
75 void IDictionary
.Add (object key
, object value)
77 Add ((PropertyDescriptor
) value);
90 void IDictionary
.Clear ()
95 public bool Contains (PropertyDescriptor
value)
97 return properties
.Contains (value);
100 bool IList
.Contains (object value)
102 return Contains ((PropertyDescriptor
) value);
105 bool IDictionary
.Contains (object value)
107 return Contains ((PropertyDescriptor
) value);
110 public void CopyTo (Array array
, int index
)
112 properties
.CopyTo (array
, index
);
115 public virtual PropertyDescriptor
Find (string name
, bool ignoreCase
)
117 foreach (PropertyDescriptor p
in properties
) {
118 if (0 == String
.Compare (name
, p
.Name
, ignoreCase
))
124 public virtual IEnumerator
GetEnumerator ()
126 return properties
.GetEnumerator ();
130 IDictionaryEnumerator IDictionary
.GetEnumerator ()
132 throw new NotImplementedException ();
135 public int IndexOf (PropertyDescriptor
value)
137 return properties
.IndexOf (value);
140 int IList
.IndexOf (object value)
142 return IndexOf ((PropertyDescriptor
) value);
145 public void Insert (int index
, PropertyDescriptor
value)
147 properties
.Insert (index
, value);
150 void IList
.Insert (int index
, object value)
152 Insert (index
, (PropertyDescriptor
) value);
155 public void Remove (PropertyDescriptor
value)
157 properties
.Remove (value);
160 void IDictionary
.Remove (object value)
162 Remove ((PropertyDescriptor
) value);
165 void IList
.Remove (object value)
167 Remove ((PropertyDescriptor
) value);
170 public void RemoveAt (int index
)
172 properties
.RemoveAt (index
);
175 void IList
.RemoveAt (int index
)
180 private PropertyDescriptorCollection
CloneCollection ()
182 PropertyDescriptorCollection col
= new PropertyDescriptorCollection ();
183 col
.properties
= (ArrayList
) properties
.Clone ();
187 public virtual PropertyDescriptorCollection
Sort ()
189 PropertyDescriptorCollection col
= CloneCollection ();
190 col
.InternalSort ((IComparer
) null);
194 public virtual PropertyDescriptorCollection
Sort (IComparer comparer
)
196 PropertyDescriptorCollection col
= CloneCollection ();
197 col
.InternalSort (comparer
);
201 public virtual PropertyDescriptorCollection
Sort (string[] order
)
203 PropertyDescriptorCollection col
= CloneCollection ();
204 col
.InternalSort (order
);
208 public virtual PropertyDescriptorCollection
Sort (string[] order
, IComparer comparer
)
210 PropertyDescriptorCollection col
= CloneCollection ();
211 ArrayList sorted
= col
.ExtractItems (order
);
212 col
.InternalSort (comparer
);
213 sorted
.AddRange (col
.properties
);
214 col
.properties
= sorted
;
218 protected void InternalSort (IComparer ic
)
220 properties
.Sort (ic
);
223 protected void InternalSort (string [] order
)
225 ArrayList sorted
= ExtractItems (order
);
226 InternalSort ((IComparer
) null);
227 sorted
.AddRange (properties
);
231 ArrayList
ExtractItems (string[] names
)
233 ArrayList sorted
= new ArrayList (properties
.Count
);
234 object[] ext
= new object [names
.Length
];
236 for (int n
=0; n
<properties
.Count
; n
++)
238 PropertyDescriptor ed
= (PropertyDescriptor
) properties
[n
];
239 int i
= Array
.IndexOf (names
, ed
.Name
);
242 properties
.RemoveAt (n
);
246 foreach (object ob
in ext
)
247 if (ob
!= null) sorted
.Add (ob
);
252 internal PropertyDescriptorCollection
Filter (Attribute
[] attributes
)
254 ArrayList list
= new ArrayList ();
255 foreach (PropertyDescriptor pd
in properties
)
256 if (pd
.Attributes
.Contains (attributes
))
259 return new PropertyDescriptorCollection (list
);
262 bool IDictionary
.IsFixedSize
269 bool IList
.IsFixedSize
276 bool IList
.IsReadOnly
283 bool IDictionary
.IsReadOnly
291 bool ICollection
.IsSynchronized
301 return properties
.Count
;
305 object ICollection
.SyncRoot
312 ICollection IDictionary
.Keys
315 string [] keys
= new string [properties
.Count
];
317 foreach (PropertyDescriptor p
in properties
)
323 ICollection IDictionary
.Values
326 return (ICollection
) properties
.Clone ();
330 object IDictionary
.this [object key
]
333 if (!(key
is string))
335 return this [(string) key
];
338 if (!(key
is string) || (value as PropertyDescriptor
) == null)
339 throw new ArgumentException ();
340 int idx
= properties
.IndexOf (value);
342 Add ((PropertyDescriptor
) value);
344 properties
[idx
] = value;
348 public virtual PropertyDescriptor
this [string s
]
351 return Find (s
, false);
355 object IList
.this [int index
]
358 return properties
[index
];
361 properties
[index
] = value;
365 public virtual PropertyDescriptor
this [int index
]
368 return (PropertyDescriptor
) properties
[index
];