BindingElementCollection.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.ServiceModel;
  7. using System.ComponentModel;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Runtime.Serialization;
  11. public class BindingElementCollection : Collection<BindingElement>
  12. {
  13. public BindingElementCollection()
  14. {
  15. }
  16. public BindingElementCollection(IEnumerable<BindingElement> elements)
  17. {
  18. if (elements == null)
  19. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("elements");
  20. foreach (BindingElement element in elements)
  21. {
  22. base.Add(element);
  23. }
  24. }
  25. public BindingElementCollection(BindingElement[] elements)
  26. {
  27. if (elements == null)
  28. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("elements");
  29. for (int i = 0; i < elements.Length; i++)
  30. {
  31. base.Add(elements[i]);
  32. }
  33. }
  34. internal BindingElementCollection(BindingElementCollection elements)
  35. {
  36. if (elements == null)
  37. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("elements");
  38. for (int i = 0; i < elements.Count; i++)
  39. {
  40. base.Add(elements[i]);
  41. }
  42. }
  43. // returns a new collection with clones of all the elements
  44. public BindingElementCollection Clone()
  45. {
  46. BindingElementCollection result = new BindingElementCollection();
  47. for (int i = 0; i < this.Count; i++)
  48. {
  49. result.Add(this[i].Clone());
  50. }
  51. return result;
  52. }
  53. public void AddRange(params BindingElement[] elements)
  54. {
  55. if (elements == null)
  56. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("elements");
  57. for (int i = 0; i < elements.Length; i++)
  58. {
  59. base.Add(elements[i]);
  60. }
  61. }
  62. public bool Contains(Type bindingElementType)
  63. {
  64. if (bindingElementType == null)
  65. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bindingElementType");
  66. for (int i = 0; i < this.Count; i++)
  67. {
  68. if (bindingElementType.IsInstanceOfType(this[i]))
  69. return true;
  70. }
  71. return false;
  72. }
  73. public T Find<T>()
  74. {
  75. return Find<T>(false);
  76. }
  77. public T Remove<T>()
  78. {
  79. return Find<T>(true);
  80. }
  81. T Find<T>(bool remove)
  82. {
  83. for (int index = 0; index < this.Count; index++)
  84. {
  85. if (this[index] is T)
  86. {
  87. T item = (T)(object)this[index];
  88. if (remove)
  89. {
  90. RemoveAt(index);
  91. }
  92. return item;
  93. }
  94. }
  95. return default(T);
  96. }
  97. public Collection<T> FindAll<T>()
  98. {
  99. return FindAll<T>(false);
  100. }
  101. public Collection<T> RemoveAll<T>()
  102. {
  103. return FindAll<T>(true);
  104. }
  105. Collection<T> FindAll<T>(bool remove)
  106. {
  107. Collection<T> collection = new Collection<T>();
  108. for (int index = 0; index < this.Count; index++)
  109. {
  110. if (this[index] is T)
  111. {
  112. T item = (T)(object)this[index];
  113. if (remove)
  114. {
  115. RemoveAt(index);
  116. // back up the index so we inspect the new item at this location
  117. index--;
  118. }
  119. collection.Add(item);
  120. }
  121. }
  122. return collection;
  123. }
  124. protected override void InsertItem(int index, BindingElement item)
  125. {
  126. if (item == null)
  127. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item");
  128. base.InsertItem(index, item);
  129. }
  130. protected override void SetItem(int index, BindingElement item)
  131. {
  132. if (item == null)
  133. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item");
  134. base.SetItem(index, item);
  135. }
  136. }
  137. }