Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace YourNamespace
  6. {
  7. /// <summary>A disposable collection of disposables.</summary>
  8. /// <seealso cref="System.IDisposable" />
  9. public class DisposableBag : IDisposable
  10. {
  11. private object _lock = new object();
  12. private List<IDisposable> _bag = new List<IDisposable>();
  13. private bool _disposed = false;
  14.  
  15. /// <summary>Creates disposable collection of disposables.</summary>
  16. /// <returns>New disposable collection of disposables</returns>
  17. public static DisposableBag Create()
  18. {
  19. return new DisposableBag();
  20. }
  21.  
  22. /// <summary>Creates disposable collection of disposables starting with provided ones.</summary>
  23. /// <param name="disposables">The disposables.</param>
  24. /// <returns>New disposable collection of disposables.</returns>
  25. public static DisposableBag Create(IEnumerable<IDisposable> disposables)
  26. {
  27. return new DisposableBag(disposables);
  28. }
  29.  
  30. /// <summary>Creates disposable collection of disposables.</summary>
  31. /// <param name="disposables">The disposables.</param>
  32. /// <returns>New disposable collection of disposables.</returns>
  33. public static DisposableBag Create(params IDisposable[] disposables)
  34. {
  35. return new DisposableBag(disposables);
  36. }
  37.  
  38. /// <summary>Initializes a new instance of the <see cref="DisposableBag"/> class.</summary>
  39. public DisposableBag()
  40. {
  41. }
  42.  
  43. /// <summary>Initializes a new instance of the <see cref="DisposableBag"/> class.</summary>
  44. /// <param name="disposables">The disposables.</param>
  45. public DisposableBag(IEnumerable<IDisposable> disposables)
  46. {
  47. lock (_lock)
  48. {
  49. AddMany(disposables);
  50. }
  51. }
  52.  
  53. /// <summary>Adds many disposables.</summary>
  54. /// <param name="disposables">The disposables.</param>
  55. public void AddMany(IEnumerable<IDisposable> disposables)
  56. {
  57. lock (_lock)
  58. {
  59. if (_disposed)
  60. {
  61. DisposeMany(disposables.ToArray());
  62. }
  63. else
  64. {
  65. _bag.AddRange(disposables);
  66. }
  67. }
  68. }
  69.  
  70. /// <summary>Disposes items.</summary>
  71. /// <param name="disposables">The disposables.</param>
  72. /// <exception cref="AggregateException">Combines exceptions thrown when disposing items.</exception>
  73. private static void DisposeMany(IDisposable[] disposables)
  74. {
  75. IList<Exception> exceptions = null;
  76. foreach (var disposable in disposables)
  77. {
  78. try
  79. {
  80. disposable.Dispose();
  81. }
  82. catch (Exception e)
  83. {
  84. (exceptions = exceptions ?? new List<Exception>()).Add(e);
  85. }
  86. }
  87.  
  88. if (exceptions != null)
  89. throw new AggregateException(exceptions);
  90. }
  91.  
  92. /// <summary>Adds disposable.</summary>
  93. /// <typeparam name="T">Type of object.</typeparam>
  94. /// <param name="item">The disposable item.</param>
  95. /// <returns>Same item for further processing.</returns>
  96. public T Add<T>(T item) where T : IDisposable
  97. {
  98. if (item == null)
  99. return item;
  100.  
  101. lock (_lock)
  102. {
  103. if (_disposed)
  104. {
  105. item.Dispose();
  106. }
  107. else
  108. {
  109. _bag.Add(item);
  110. }
  111. return item;
  112. }
  113. }
  114.  
  115. /// <summary>Performs application-defined tasks associated with
  116. /// freeing, releasing, or resetting unmanaged resources.</summary>
  117. public void Dispose()
  118. {
  119. lock (_lock)
  120. {
  121. _disposed = true;
  122. var disposables = _bag.ToArray();
  123. _bag.Clear();
  124. DisposeMany(disposables);
  125. }
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement