PropertyDescriptorCollection.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //
  2. // System.ComponentModel.PropertyDescriptorCollection.cs
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (C) Rodrigo Moya, 2002
  10. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  11. // (C) 2003 Andreas Nahr
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Collections;
  34. namespace System.ComponentModel
  35. {
  36. /// <summary>
  37. /// Represents a collection of PropertyDescriptor objects.
  38. /// </summary>
  39. //[DefaultMember ("Item")]
  40. public class PropertyDescriptorCollection : IList, ICollection, IEnumerable, IDictionary
  41. {
  42. public static readonly PropertyDescriptorCollection Empty = new PropertyDescriptorCollection ((ArrayList)null);
  43. ArrayList properties;
  44. internal PropertyDescriptorCollection (ArrayList list)
  45. {
  46. properties = list;
  47. }
  48. public PropertyDescriptorCollection (PropertyDescriptor[] properties)
  49. {
  50. this.properties = new ArrayList ();
  51. if (properties == null)
  52. return;
  53. this.properties.AddRange (properties);
  54. }
  55. private PropertyDescriptorCollection ()
  56. {
  57. }
  58. public int Add (PropertyDescriptor value)
  59. {
  60. properties.Add (value);
  61. return properties.Count - 1;
  62. }
  63. int IList.Add (object value)
  64. {
  65. return Add ((PropertyDescriptor) value);
  66. }
  67. void IDictionary.Add (object key, object value)
  68. {
  69. Add ((PropertyDescriptor) value);
  70. }
  71. public void Clear ()
  72. {
  73. properties.Clear ();
  74. }
  75. void IList.Clear ()
  76. {
  77. Clear ();
  78. }
  79. void IDictionary.Clear ()
  80. {
  81. Clear ();
  82. }
  83. public bool Contains (PropertyDescriptor value)
  84. {
  85. return properties.Contains (value);
  86. }
  87. bool IList.Contains (object value)
  88. {
  89. return Contains ((PropertyDescriptor) value);
  90. }
  91. bool IDictionary.Contains (object value)
  92. {
  93. return Contains ((PropertyDescriptor) value);
  94. }
  95. public void CopyTo (Array array, int index)
  96. {
  97. properties.CopyTo (array, index);
  98. }
  99. public virtual PropertyDescriptor Find (string name, bool ignoreCase)
  100. {
  101. foreach (PropertyDescriptor p in properties) {
  102. if (0 == String.Compare (name, p.Name, ignoreCase))
  103. return p;
  104. }
  105. return null;
  106. }
  107. public virtual IEnumerator GetEnumerator ()
  108. {
  109. return properties.GetEnumerator ();
  110. }
  111. [MonoTODO]
  112. IDictionaryEnumerator IDictionary.GetEnumerator ()
  113. {
  114. throw new NotImplementedException ();
  115. }
  116. public int IndexOf (PropertyDescriptor value)
  117. {
  118. return properties.IndexOf (value);
  119. }
  120. int IList.IndexOf (object value)
  121. {
  122. return IndexOf ((PropertyDescriptor) value);
  123. }
  124. public void Insert (int index, PropertyDescriptor value)
  125. {
  126. properties.Insert (index, value);
  127. }
  128. void IList.Insert (int index, object value)
  129. {
  130. Insert (index, (PropertyDescriptor) value);
  131. }
  132. public void Remove (PropertyDescriptor value)
  133. {
  134. properties.Remove (value);
  135. }
  136. void IDictionary.Remove (object value)
  137. {
  138. Remove ((PropertyDescriptor) value);
  139. }
  140. void IList.Remove (object value)
  141. {
  142. Remove ((PropertyDescriptor) value);
  143. }
  144. public void RemoveAt (int index)
  145. {
  146. properties.RemoveAt (index);
  147. }
  148. void IList.RemoveAt (int index)
  149. {
  150. RemoveAt (index);
  151. }
  152. private PropertyDescriptorCollection CloneCollection ()
  153. {
  154. PropertyDescriptorCollection col = new PropertyDescriptorCollection ();
  155. col.properties = (ArrayList) properties.Clone ();
  156. return col;
  157. }
  158. public virtual PropertyDescriptorCollection Sort ()
  159. {
  160. PropertyDescriptorCollection col = CloneCollection ();
  161. col.InternalSort ((IComparer) null);
  162. return col;
  163. }
  164. public virtual PropertyDescriptorCollection Sort (IComparer comparer)
  165. {
  166. PropertyDescriptorCollection col = CloneCollection ();
  167. col.InternalSort (comparer);
  168. return col;
  169. }
  170. public virtual PropertyDescriptorCollection Sort (string[] order)
  171. {
  172. PropertyDescriptorCollection col = CloneCollection ();
  173. col.InternalSort (order);
  174. return col;
  175. }
  176. public virtual PropertyDescriptorCollection Sort (string[] order, IComparer comparer)
  177. {
  178. PropertyDescriptorCollection col = CloneCollection ();
  179. ArrayList sorted = col.ExtractItems (order);
  180. col.InternalSort (comparer);
  181. sorted.AddRange (col.properties);
  182. col.properties = sorted;
  183. return col;
  184. }
  185. protected void InternalSort (IComparer ic)
  186. {
  187. properties.Sort (ic);
  188. }
  189. protected void InternalSort (string [] order)
  190. {
  191. ArrayList sorted = ExtractItems (order);
  192. InternalSort ((IComparer) null);
  193. sorted.AddRange (properties);
  194. properties = sorted;
  195. }
  196. ArrayList ExtractItems (string[] names)
  197. {
  198. ArrayList sorted = new ArrayList (properties.Count);
  199. object[] ext = new object [names.Length];
  200. for (int n=0; n<properties.Count; n++)
  201. {
  202. PropertyDescriptor ed = (PropertyDescriptor) properties[n];
  203. int i = Array.IndexOf (names, ed.Name);
  204. if (i != -1) {
  205. ext[i] = ed;
  206. properties.RemoveAt (n);
  207. n--;
  208. }
  209. }
  210. foreach (object ob in ext)
  211. if (ob != null) sorted.Add (ob);
  212. return sorted;
  213. }
  214. internal PropertyDescriptorCollection Filter (Attribute[] attributes)
  215. {
  216. ArrayList list = new ArrayList ();
  217. foreach (PropertyDescriptor pd in properties)
  218. if (pd.Attributes.Contains (attributes))
  219. list.Add (pd);
  220. return new PropertyDescriptorCollection (list);
  221. }
  222. bool IDictionary.IsFixedSize
  223. {
  224. get {
  225. return true;
  226. }
  227. }
  228. bool IList.IsFixedSize
  229. {
  230. get {
  231. return true;
  232. }
  233. }
  234. bool IList.IsReadOnly
  235. {
  236. get {
  237. return false;
  238. }
  239. }
  240. bool IDictionary.IsReadOnly
  241. {
  242. get
  243. {
  244. return false;
  245. }
  246. }
  247. bool ICollection.IsSynchronized
  248. {
  249. get {
  250. return false;
  251. }
  252. }
  253. public int Count
  254. {
  255. get {
  256. return properties.Count;
  257. }
  258. }
  259. object ICollection.SyncRoot
  260. {
  261. get {
  262. return null;
  263. }
  264. }
  265. ICollection IDictionary.Keys
  266. {
  267. get {
  268. string [] keys = new string [properties.Count];
  269. int i = 0;
  270. foreach (PropertyDescriptor p in properties)
  271. keys [i++] = p.Name;
  272. return keys;
  273. }
  274. }
  275. ICollection IDictionary.Values
  276. {
  277. get {
  278. return (ICollection) properties.Clone ();
  279. }
  280. }
  281. object IDictionary.this [object key]
  282. {
  283. get {
  284. if (!(key is string))
  285. return null;
  286. return this [(string) key];
  287. }
  288. set {
  289. if (!(key is string) || (value as PropertyDescriptor) == null)
  290. throw new ArgumentException ();
  291. int idx = properties.IndexOf (value);
  292. if (idx == -1)
  293. Add ((PropertyDescriptor) value);
  294. else
  295. properties [idx] = value;
  296. }
  297. }
  298. public virtual PropertyDescriptor this [string s]
  299. {
  300. get {
  301. return Find (s, false);
  302. }
  303. }
  304. object IList.this [int index]
  305. {
  306. get {
  307. return properties [index];
  308. }
  309. set {
  310. properties [index] = value;
  311. }
  312. }
  313. public virtual PropertyDescriptor this [int index]
  314. {
  315. get {
  316. return (PropertyDescriptor) properties [index];
  317. }
  318. }
  319. }
  320. }