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
);
55 public PropertyDescriptorCollection (PropertyDescriptor
[] properties
, bool readOnly
) : this (properties
)
57 this.readOnly
= readOnly
;
60 private PropertyDescriptorCollection ()
64 public int Add (PropertyDescriptor
value)
67 throw new NotSupportedException ();
69 properties
.Add (value);
70 return properties
.Count
- 1;
73 int IList
.Add (object value)
75 return Add ((PropertyDescriptor
) value);
78 void IDictionary
.Add (object key
, object value)
80 if ((value as PropertyDescriptor
) == null) {
81 throw new ArgumentException ("value");
84 Add ((PropertyDescriptor
) value);
90 throw new NotSupportedException ();
100 void IDictionary
.Clear ()
105 public bool Contains (PropertyDescriptor
value)
107 return properties
.Contains (value);
111 bool IList
.Contains (object value)
113 return Contains ((PropertyDescriptor
) value);
116 bool IDictionary
.Contains (object value)
118 return Contains ((PropertyDescriptor
) value);
121 public void CopyTo (Array array
, int index
)
123 properties
.CopyTo (array
, index
);
126 public virtual PropertyDescriptor
Find (string name
, bool ignoreCase
)
129 throw new ArgumentNullException ("name");
131 for (int i
= 0; i
< properties
.Count
; ++i
) {
132 PropertyDescriptor p
= (PropertyDescriptor
)properties
[i
];
134 if (0 == String
.Compare (name
, p
.Name
, StringComparison
.OrdinalIgnoreCase
))
138 if (0 == String
.Compare (name
, p
.Name
, StringComparison
.Ordinal
))
145 public virtual IEnumerator
GetEnumerator ()
147 return properties
.GetEnumerator ();
150 IEnumerator IEnumerable
.GetEnumerator ()
152 return GetEnumerator ();
156 IDictionaryEnumerator IDictionary
.GetEnumerator ()
158 throw new NotImplementedException ();
161 public int IndexOf (PropertyDescriptor
value)
163 return properties
.IndexOf (value);
166 int IList
.IndexOf (object value)
168 return IndexOf ((PropertyDescriptor
) value);
171 public void Insert (int index
, PropertyDescriptor
value)
174 throw new NotSupportedException ();
176 properties
.Insert (index
, value);
179 void IList
.Insert (int index
, object value)
181 Insert (index
, (PropertyDescriptor
) value);
184 public void Remove (PropertyDescriptor
value)
187 throw new NotSupportedException ();
189 properties
.Remove (value);
192 void IDictionary
.Remove (object value)
194 Remove ((PropertyDescriptor
) value);
197 void IList
.Remove (object value)
199 Remove ((PropertyDescriptor
) value);
201 public void RemoveAt (int index
)
204 throw new NotSupportedException ();
206 properties
.RemoveAt (index
);
209 void IList
.RemoveAt (int index
)
214 private PropertyDescriptorCollection
CloneCollection ()
216 PropertyDescriptorCollection col
= new PropertyDescriptorCollection ();
217 col
.properties
= (ArrayList
) properties
.Clone ();
221 public virtual PropertyDescriptorCollection
Sort ()
223 PropertyDescriptorCollection col
= CloneCollection ();
224 col
.InternalSort ((IComparer
) null);
228 public virtual PropertyDescriptorCollection
Sort (IComparer comparer
)
230 PropertyDescriptorCollection col
= CloneCollection ();
231 col
.InternalSort (comparer
);
235 public virtual PropertyDescriptorCollection
Sort (string[] order
)
237 PropertyDescriptorCollection col
= CloneCollection ();
238 col
.InternalSort (order
);
242 public virtual PropertyDescriptorCollection
Sort (string[] order
, IComparer comparer
)
244 PropertyDescriptorCollection col
= CloneCollection ();
246 ArrayList sorted
= col
.ExtractItems (order
);
247 col
.InternalSort (comparer
);
248 sorted
.AddRange (col
.properties
);
249 col
.properties
= sorted
;
251 col
.InternalSort (comparer
);
256 protected void InternalSort (IComparer ic
)
259 ic
= MemberDescriptor
.DefaultComparer
;
260 properties
.Sort (ic
);
263 protected void InternalSort (string [] order
)
266 ArrayList sorted
= ExtractItems (order
);
267 InternalSort ((IComparer
) null);
268 sorted
.AddRange (properties
);
271 InternalSort ((IComparer
) null);
275 ArrayList
ExtractItems (string[] names
)
277 ArrayList sorted
= new ArrayList (properties
.Count
);
278 object[] ext
= new object [names
.Length
];
280 for (int n
=0; n
<properties
.Count
; n
++)
282 PropertyDescriptor ed
= (PropertyDescriptor
) properties
[n
];
283 int i
= Array
.IndexOf (names
, ed
.Name
);
286 properties
.RemoveAt (n
);
290 foreach (object ob
in ext
)
291 if (ob
!= null) sorted
.Add (ob
);
296 internal PropertyDescriptorCollection
Filter (Attribute
[] attributes
)
298 ArrayList list
= new ArrayList ();
299 foreach (PropertyDescriptor pd
in properties
) {
300 if (pd
.Attributes
.Contains (attributes
)) {
304 PropertyDescriptor
[] descriptors
= new PropertyDescriptor
[list
.Count
];
305 list
.CopyTo (descriptors
);
306 return new PropertyDescriptorCollection (descriptors
, true);
309 bool IDictionary
.IsFixedSize
311 get {return ((IList)this).IsFixedSize;}
313 bool IList
.IsFixedSize
320 bool IDictionary
.IsReadOnly
322 get {return ((IList)this).IsReadOnly;}
324 bool IList
.IsReadOnly
332 bool ICollection
.IsSynchronized
339 int ICollection
.Count
{
340 get { return Count; }
346 return properties
.Count
;
350 object ICollection
.SyncRoot
357 ICollection IDictionary
.Keys
360 string [] keys
= new string [properties
.Count
];
362 foreach (PropertyDescriptor p
in properties
)
368 ICollection IDictionary
.Values
371 return (ICollection
) properties
.Clone ();
375 object IDictionary
.this [object key
]
378 if (!(key
is string))
380 return this [(string) key
];
384 throw new NotSupportedException ();
387 if (!(key
is string) || (value as PropertyDescriptor
) == null)
388 throw new ArgumentException ();
389 int idx
= properties
.IndexOf (value);
391 Add ((PropertyDescriptor
) value);
393 properties
[idx
] = value;
397 public virtual PropertyDescriptor
this [string s
]
400 return Find (s
, false);
404 object IList
.this [int index
]
407 return properties
[index
];
411 throw new NotSupportedException ();
413 properties
[index
] = value;
417 public virtual PropertyDescriptor
this [int index
]
420 return (PropertyDescriptor
) properties
[index
];