ExtensionCollection.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Runtime.Serialization;
  10. public sealed class ExtensionCollection<T> : SynchronizedCollection<IExtension<T>>, IExtensionCollection<T>
  11. where T : IExtensibleObject<T>
  12. {
  13. T owner;
  14. public ExtensionCollection(T owner)
  15. {
  16. if (owner == null)
  17. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("owner");
  18. this.owner = owner;
  19. }
  20. public ExtensionCollection(T owner, object syncRoot)
  21. : base(syncRoot)
  22. {
  23. if (owner == null)
  24. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("owner");
  25. this.owner = owner;
  26. }
  27. bool ICollection<IExtension<T>>.IsReadOnly
  28. {
  29. get { return false; }
  30. }
  31. protected override void ClearItems()
  32. {
  33. IExtension<T>[] array;
  34. lock (this.SyncRoot)
  35. {
  36. array = new IExtension<T>[this.Count];
  37. this.CopyTo(array, 0);
  38. base.ClearItems();
  39. foreach (IExtension<T> extension in array)
  40. {
  41. extension.Detach(this.owner);
  42. }
  43. }
  44. }
  45. public E Find<E>()
  46. {
  47. List<IExtension<T>> items = this.Items;
  48. lock (this.SyncRoot)
  49. {
  50. for (int i = this.Count - 1; i >= 0; i--)
  51. {
  52. IExtension<T> item = items[i];
  53. if (item is E)
  54. return (E)item;
  55. }
  56. }
  57. return default(E);
  58. }
  59. public Collection<E> FindAll<E>()
  60. {
  61. Collection<E> result = new Collection<E>();
  62. List<IExtension<T>> items = this.Items;
  63. lock (this.SyncRoot)
  64. {
  65. for (int i = 0; i < items.Count; i++)
  66. {
  67. IExtension<T> item = items[i];
  68. if (item is E)
  69. result.Add((E)item);
  70. }
  71. }
  72. return result;
  73. }
  74. protected override void InsertItem(int index, IExtension<T> item)
  75. {
  76. if (item == null)
  77. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item");
  78. lock (this.SyncRoot)
  79. {
  80. item.Attach(this.owner);
  81. base.InsertItem(index, item);
  82. }
  83. }
  84. protected override void RemoveItem(int index)
  85. {
  86. lock (this.SyncRoot)
  87. {
  88. this.Items[index].Detach(this.owner);
  89. base.RemoveItem(index);
  90. }
  91. }
  92. protected override void SetItem(int index, IExtension<T> item)
  93. {
  94. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxCannotSetExtensionsByIndex)));
  95. }
  96. }
  97. }