mirror of
https://github.com/ldqk/Masuit.Tools.git
synced 2026-10-27 11:16:34 +08:00
68 lines
1.3 KiB
C#
68 lines
1.3 KiB
C#
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();
|
|
}
|
|
}
|
|
}
|