PropertyDescriptorCollection.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. public class PropertyDescriptorCollection : IList, ICollection, IEnumerable, IDictionary
  40. {
  41. public static readonly PropertyDescriptorCollection Empty = new PropertyDescriptorCollection (null, true);
  42. private ArrayList properties;
  43. private bool readOnly;
  44. public PropertyDescriptorCollection (PropertyDescriptor[] properties)
  45. {
  46. this.properties = new ArrayList ();
  47. if (properties == null)
  48. return;
  49. this.properties.AddRange (properties);
  50. }
  51. #if NET_2_0
  52. public
  53. #else
  54. internal
  55. #endif
  56. PropertyDescriptorCollection (PropertyDescriptor[] properties, bool readOnly) : this (properties)
  57. {
  58. this.readOnly = readOnly;
  59. }
  60. private PropertyDescriptorCollection ()
  61. {
  62. }
  63. public int Add (PropertyDescriptor value)
  64. {
  65. if (readOnly) {
  66. throw new NotSupportedException ();
  67. }
  68. properties.Add (value);
  69. return properties.Count - 1;
  70. }
  71. int IList.Add (object value)
  72. {
  73. return Add ((PropertyDescriptor) value);
  74. }
  75. void IDictionary.Add (object key, object value)
  76. {
  77. if ((value as PropertyDescriptor) == null) {
  78. throw new ArgumentException ("value");
  79. }
  80. Add ((PropertyDescriptor) value);
  81. }
  82. public void Clear ()
  83. {
  84. if (readOnly) {
  85. throw new NotSupportedException ();
  86. }
  87. properties.Clear ();
  88. }
  89. #if !TARGET_JVM // DUAL_IFACE_CONFLICT
  90. void IList.Clear ()
  91. {
  92. Clear ();
  93. }
  94. void IDictionary.Clear ()
  95. {
  96. Clear ();
  97. }
  98. #endif
  99. public bool Contains (PropertyDescriptor value)
  100. {
  101. return properties.Contains (value);
  102. }
  103. #if TARGET_JVM // DUAL_IFACE_CONFLICT
  104. public bool Contains (object value)
  105. {
  106. return Contains ((PropertyDescriptor) value);
  107. }
  108. #else
  109. bool IList.Contains (object value)
  110. {
  111. return Contains ((PropertyDescriptor) value);
  112. }
  113. bool IDictionary.Contains (object value)
  114. {
  115. return Contains ((PropertyDescriptor) value);
  116. }
  117. #endif
  118. public void CopyTo (Array array, int index)
  119. {
  120. properties.CopyTo (array, index);
  121. }
  122. public virtual PropertyDescriptor Find (string name, bool ignoreCase)
  123. {
  124. if (name == null) {
  125. throw new ArgumentNullException ("name");
  126. }
  127. foreach (PropertyDescriptor p in properties) {
  128. #if NET_2_0
  129. if (ignoreCase) {
  130. if (0 == String.Compare (name, p.Name, StringComparison.OrdinalIgnoreCase))
  131. return p;
  132. }
  133. else {
  134. if (0 == String.Compare (name, p.Name, StringComparison.Ordinal))
  135. return p;
  136. }
  137. #else
  138. if (ignoreCase) {
  139. if (0 == String.CompareOrdinal (name.ToLower (System.Globalization.CultureInfo.InvariantCulture),
  140. p.Name.ToLower (System.Globalization.CultureInfo.InvariantCulture)))
  141. return p;
  142. }
  143. else {
  144. if (0 == String.CompareOrdinal (name, p.Name))
  145. return p;
  146. }
  147. #endif
  148. }
  149. return null;
  150. }
  151. public virtual IEnumerator GetEnumerator ()
  152. {
  153. return properties.GetEnumerator ();
  154. }
  155. IEnumerator IEnumerable.GetEnumerator ()
  156. {
  157. return GetEnumerator ();
  158. }
  159. [MonoTODO]
  160. IDictionaryEnumerator IDictionary.GetEnumerator ()
  161. {
  162. throw new NotImplementedException ();
  163. }
  164. public int IndexOf (PropertyDescriptor value)
  165. {
  166. return properties.IndexOf (value);
  167. }
  168. int IList.IndexOf (object value)
  169. {
  170. return IndexOf ((PropertyDescriptor) value);
  171. }
  172. public void Insert (int index, PropertyDescriptor value)
  173. {
  174. if (readOnly) {
  175. throw new NotSupportedException ();
  176. }
  177. properties.Insert (index, value);
  178. }
  179. void IList.Insert (int index, object value)
  180. {
  181. Insert (index, (PropertyDescriptor) value);
  182. }
  183. public void Remove (PropertyDescriptor value)
  184. {
  185. if (readOnly) {
  186. throw new NotSupportedException ();
  187. }
  188. properties.Remove (value);
  189. }
  190. #if TARGET_JVM// DUAL_IFACE_CONFLICT
  191. public void Remove (object value)
  192. {
  193. Remove ((PropertyDescriptor) value);
  194. }
  195. #else
  196. void IDictionary.Remove (object value)
  197. {
  198. Remove ((PropertyDescriptor) value);
  199. }
  200. void IList.Remove (object value)
  201. {
  202. Remove ((PropertyDescriptor) value);
  203. }
  204. #endif
  205. public void RemoveAt (int index)
  206. {
  207. if (readOnly) {
  208. throw new NotSupportedException ();
  209. }
  210. properties.RemoveAt (index);
  211. }
  212. void IList.RemoveAt (int index)
  213. {
  214. RemoveAt (index);
  215. }
  216. private PropertyDescriptorCollection CloneCollection ()
  217. {
  218. PropertyDescriptorCollection col = new PropertyDescriptorCollection ();
  219. col.properties = (ArrayList) properties.Clone ();
  220. return col;
  221. }
  222. public virtual PropertyDescriptorCollection Sort ()
  223. {
  224. PropertyDescriptorCollection col = CloneCollection ();
  225. col.InternalSort ((IComparer) null);
  226. return col;
  227. }
  228. public virtual PropertyDescriptorCollection Sort (IComparer comparer)
  229. {
  230. PropertyDescriptorCollection col = CloneCollection ();
  231. col.InternalSort (comparer);
  232. return col;
  233. }
  234. public virtual PropertyDescriptorCollection Sort (string[] order)
  235. {
  236. PropertyDescriptorCollection col = CloneCollection ();
  237. col.InternalSort (order);
  238. return col;
  239. }
  240. public virtual PropertyDescriptorCollection Sort (string[] order, IComparer comparer)
  241. {
  242. PropertyDescriptorCollection col = CloneCollection ();
  243. ArrayList sorted = col.ExtractItems (order);
  244. col.InternalSort (comparer);
  245. sorted.AddRange (col.properties);
  246. col.properties = sorted;
  247. return col;
  248. }
  249. protected void InternalSort (IComparer ic)
  250. {
  251. properties.Sort (ic);
  252. }
  253. protected void InternalSort (string [] order)
  254. {
  255. ArrayList sorted = ExtractItems (order);
  256. InternalSort ((IComparer) null);
  257. sorted.AddRange (properties);
  258. properties = sorted;
  259. }
  260. ArrayList ExtractItems (string[] names)
  261. {
  262. ArrayList sorted = new ArrayList (properties.Count);
  263. object[] ext = new object [names.Length];
  264. for (int n=0; n<properties.Count; n++)
  265. {
  266. PropertyDescriptor ed = (PropertyDescriptor) properties[n];
  267. int i = Array.IndexOf (names, ed.Name);
  268. if (i != -1) {
  269. ext[i] = ed;
  270. properties.RemoveAt (n);
  271. n--;
  272. }
  273. }
  274. foreach (object ob in ext)
  275. if (ob != null) sorted.Add (ob);
  276. return sorted;
  277. }
  278. internal PropertyDescriptorCollection Filter (Attribute[] attributes)
  279. {
  280. ArrayList list = new ArrayList ();
  281. foreach (PropertyDescriptor pd in properties) {
  282. if (pd.Attributes.Contains (attributes)) {
  283. list.Add (pd);
  284. }
  285. }
  286. PropertyDescriptor[] descriptors = new PropertyDescriptor[list.Count];
  287. list.CopyTo (descriptors);
  288. return new PropertyDescriptorCollection (descriptors, true);
  289. }
  290. #if TARGET_JVM //DUAL_IFACE_CONFLICT
  291. public bool IsFixedSize
  292. #else
  293. bool IDictionary.IsFixedSize
  294. {
  295. get {return ((IList)this).IsFixedSize;}
  296. }
  297. bool IList.IsFixedSize
  298. #endif
  299. {
  300. get
  301. {
  302. #if NET_2_0
  303. return readOnly;
  304. #else
  305. return !readOnly;
  306. #endif
  307. }
  308. }
  309. #if TARGET_JVM //DUAL_IFACE_CONFLICT
  310. public bool IsReadOnly
  311. #else
  312. bool IDictionary.IsReadOnly
  313. {
  314. get {return ((IList)this).IsReadOnly;}
  315. }
  316. bool IList.IsReadOnly
  317. #endif
  318. {
  319. get
  320. {
  321. return readOnly;
  322. }
  323. }
  324. bool ICollection.IsSynchronized
  325. {
  326. get {
  327. return false;
  328. }
  329. }
  330. int ICollection.Count {
  331. get { return Count; }
  332. }
  333. public int Count
  334. {
  335. get {
  336. return properties.Count;
  337. }
  338. }
  339. object ICollection.SyncRoot
  340. {
  341. get {
  342. return null;
  343. }
  344. }
  345. ICollection IDictionary.Keys
  346. {
  347. get {
  348. string [] keys = new string [properties.Count];
  349. int i = 0;
  350. foreach (PropertyDescriptor p in properties)
  351. keys [i++] = p.Name;
  352. return keys;
  353. }
  354. }
  355. ICollection IDictionary.Values
  356. {
  357. get {
  358. return (ICollection) properties.Clone ();
  359. }
  360. }
  361. object IDictionary.this [object key]
  362. {
  363. get {
  364. if (!(key is string))
  365. return null;
  366. return this [(string) key];
  367. }
  368. set {
  369. if (readOnly) {
  370. throw new NotSupportedException ();
  371. }
  372. if (!(key is string) || (value as PropertyDescriptor) == null)
  373. throw new ArgumentException ();
  374. int idx = properties.IndexOf (value);
  375. if (idx == -1)
  376. Add ((PropertyDescriptor) value);
  377. else
  378. properties [idx] = value;
  379. }
  380. }
  381. public virtual PropertyDescriptor this [string s]
  382. {
  383. get {
  384. return Find (s, false);
  385. }
  386. }
  387. object IList.this [int index]
  388. {
  389. get {
  390. return properties [index];
  391. }
  392. set {
  393. if (readOnly) {
  394. throw new NotSupportedException ();
  395. }
  396. properties [index] = value;
  397. }
  398. }
  399. public virtual PropertyDescriptor this [int index]
  400. {
  401. get {
  402. return (PropertyDescriptor) properties [index];
  403. }
  404. }
  405. }
  406. }