12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Masuit.Tools.Systems;
- /// <summary>
- /// 值可被Dispose的字典类型
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- public class DisposableDictionary<TKey, TValue> : NullableDictionary<TKey, TValue>, IDisposable where TValue : IDisposable
- {
- private bool _isDisposed;
- /// <summary>
- /// 终结器
- /// </summary>
- ~DisposableDictionary()
- {
- Dispose(false);
- }
- /// <summary>
- ///
- /// </summary>
- public void Dispose()
- {
- if (_isDisposed)
- {
- return;
- }
- Dispose(true);
- _isDisposed = true;
- GC.SuppressFinalize(this);
- }
- public DisposableDictionary()
- {
- }
- public DisposableDictionary(TValue fallbackValue) : base()
- {
- FallbackValue = fallbackValue;
- }
- public DisposableDictionary(int capacity) : base(capacity)
- {
- }
- public DisposableDictionary(IDictionary<NullObject<TKey>, TValue> dictionary) : base(dictionary)
- {
- }
- /// <summary>
- /// 释放
- /// </summary>
- /// <param name="disposing"></param>
- public void Dispose(bool disposing)
- {
- foreach (var s in Values.Where(v => v != null))
- {
- s.Dispose();
- }
- }
- }
|