ServiceModelConfigurationElementCollection.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Configuration;
  10. using System.Globalization;
  11. using System.Text;
  12. public abstract class ServiceModelConfigurationElementCollection<ConfigurationElementType> : ConfigurationElementCollection
  13. where ConfigurationElementType : ConfigurationElement, new()
  14. {
  15. ConfigurationElementCollectionType collectionType;
  16. string elementName;
  17. internal ServiceModelConfigurationElementCollection()
  18. : this(ConfigurationElementCollectionType.AddRemoveClearMap, null)
  19. { }
  20. internal ServiceModelConfigurationElementCollection(ConfigurationElementCollectionType collectionType,
  21. string elementName)
  22. {
  23. this.collectionType = collectionType;
  24. this.elementName = elementName;
  25. if (!String.IsNullOrEmpty(elementName))
  26. {
  27. this.AddElementName = elementName;
  28. }
  29. }
  30. internal ServiceModelConfigurationElementCollection(ConfigurationElementCollectionType collectionType,
  31. string elementName, IComparer comparer) : base(comparer)
  32. {
  33. this.collectionType = collectionType;
  34. this.elementName = elementName;
  35. }
  36. protected override void BaseAdd(ConfigurationElement element)
  37. {
  38. if (!this.IsReadOnly() && !this.ThrowOnDuplicate)
  39. {
  40. object key = this.GetElementKey(element);
  41. if (this.ContainsKey(key))
  42. {
  43. this.BaseRemove(key);
  44. }
  45. }
  46. base.BaseAdd(element);
  47. }
  48. public void Add(ConfigurationElementType element)
  49. {
  50. if (!this.IsReadOnly())
  51. {
  52. if (element == null)
  53. {
  54. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
  55. }
  56. }
  57. this.BaseAdd(element);
  58. }
  59. public void Clear()
  60. {
  61. this.BaseClear();
  62. }
  63. public override ConfigurationElementCollectionType CollectionType
  64. {
  65. get { return this.collectionType; }
  66. }
  67. public virtual bool ContainsKey(object key)
  68. {
  69. if (key == null)
  70. {
  71. List<string> elementKeys = new List<string>();
  72. ConfigurationElement dummyElement = this.CreateNewElement();
  73. foreach (PropertyInformation propertyInfo in dummyElement.ElementInformation.Properties)
  74. {
  75. if (propertyInfo.IsKey)
  76. {
  77. elementKeys.Add(propertyInfo.Name);
  78. }
  79. }
  80. if (0 == elementKeys.Count)
  81. {
  82. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("key");
  83. }
  84. else if (1 == elementKeys.Count)
  85. {
  86. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  87. SR.GetString(SR.ConfigElementKeyNull, elementKeys[0])));
  88. }
  89. else
  90. {
  91. StringBuilder elementKeysString = new StringBuilder();
  92. for (int i = 0; i < elementKeys.Count - 1; i++)
  93. {
  94. elementKeysString = elementKeysString.Append(elementKeys[i] + ", ");
  95. }
  96. elementKeysString = elementKeysString.Append(elementKeys[elementKeys.Count - 1]);
  97. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  98. SR.GetString(SR.ConfigElementKeysNull, elementKeys.ToString())));
  99. }
  100. }
  101. else
  102. {
  103. return null != this.BaseGet(key);
  104. }
  105. }
  106. protected override ConfigurationElement CreateNewElement()
  107. {
  108. return new ConfigurationElementType();
  109. }
  110. public void CopyTo(ConfigurationElementType[] array, int start)
  111. {
  112. if (array == null)
  113. {
  114. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("array");
  115. }
  116. if (start < 0 || start >= array.Length)
  117. {
  118. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("start", SR.GetString(SR.ConfigInvalidStartValue,
  119. array.Length - 1,
  120. start));
  121. }
  122. ((ICollection)this).CopyTo(array, start);
  123. }
  124. protected override string ElementName
  125. {
  126. get
  127. {
  128. string retval = this.elementName;
  129. if (string.IsNullOrEmpty(retval))
  130. {
  131. retval = base.ElementName;
  132. }
  133. return retval;
  134. }
  135. }
  136. public int IndexOf(ConfigurationElementType element)
  137. {
  138. if (element == null)
  139. {
  140. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
  141. }
  142. return this.BaseIndexOf(element);
  143. }
  144. public void Remove(ConfigurationElementType element)
  145. {
  146. if (!this.IsReadOnly())
  147. {
  148. if (element == null)
  149. {
  150. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
  151. }
  152. }
  153. this.BaseRemove(this.GetElementKey(element));
  154. }
  155. public void RemoveAt(object key)
  156. {
  157. if (!this.IsReadOnly())
  158. {
  159. if (key == null)
  160. {
  161. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("key");
  162. }
  163. }
  164. this.BaseRemove(key);
  165. }
  166. public void RemoveAt(int index)
  167. {
  168. this.BaseRemoveAt(index);
  169. }
  170. public virtual ConfigurationElementType this[object key]
  171. {
  172. get
  173. {
  174. if (key == null)
  175. {
  176. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("key");
  177. }
  178. ConfigurationElementType retval = (ConfigurationElementType)this.BaseGet(key);
  179. if (retval == null)
  180. {
  181. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new System.Collections.Generic.KeyNotFoundException(
  182. SR.GetString(SR.ConfigKeyNotFoundInElementCollection,
  183. key.ToString())));
  184. }
  185. return retval;
  186. }
  187. set
  188. {
  189. if (this.IsReadOnly())
  190. {
  191. this.Add(value);
  192. }
  193. if (value == null)
  194. {
  195. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  196. }
  197. if (key == null)
  198. {
  199. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("key");
  200. }
  201. if (this.GetElementKey(value).ToString().Equals((string)key, StringComparison.Ordinal))
  202. {
  203. if (this.BaseGet(key) != null)
  204. {
  205. this.BaseRemove(key);
  206. }
  207. this.Add(value);
  208. }
  209. else
  210. {
  211. #pragma warning disable 56506 //[....]; Variable 'key' checked for null previously
  212. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.ConfigKeysDoNotMatch,
  213. this.GetElementKey(value).ToString(),
  214. key.ToString()));
  215. #pragma warning restore
  216. }
  217. }
  218. }
  219. public ConfigurationElementType this[int index]
  220. {
  221. get
  222. {
  223. return (ConfigurationElementType)this.BaseGet(index);
  224. }
  225. set
  226. {
  227. if (!this.IsReadOnly() && !this.ThrowOnDuplicate)
  228. {
  229. if (this.BaseGet(index) != null)
  230. {
  231. this.BaseRemoveAt(index);
  232. }
  233. }
  234. this.BaseAdd(index, value);
  235. }
  236. }
  237. }
  238. }