using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace MJTop.Data
{
///
/// 忽略Key值大小写的字典集合
///
/// Value类型
public class IgCaseDictionary : Dictionary
{
///
/// 大写/小写
///
public KeyCase Case { get; private set; } = KeyCase.Lower;
private ReaderWriterLockSlim Locker { get; set; }
///
/// 构造函数,默认全部小写
///
public IgCaseDictionary()
{
this.Case = KeyCase.Lower;
this.Locker = new ReaderWriterLockSlim();
}
///
/// 构造函数,指定存储 大写?小写
///
/// 大写/小写
public IgCaseDictionary(KeyCase keyCase)
{
this.Case = keyCase;
this.Locker = new ReaderWriterLockSlim();
}
///
/// 添加
///
///
///
public new void Add(string key, TValue value)
{
try
{
Locker.EnterWriteLock();
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("key不能为空![" + key + "]", "key");
}
base.Add((Case == KeyCase.Lower ? key.ToLower() : key.ToUpper()), value);
}
catch (Exception ex)
{
LogUtils.LogError("IgCaseDictionary", Developer.SysDefault, ex, "Key:" + key + "Value:" + value);
throw ex;
}
finally
{
Locker.ExitWriteLock();
}
}
public new TValue this[string key]
{
get
{
try
{
Locker.EnterReadLock();
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("key不能为空![" + key + "]", "key");
}
return base[(Case == KeyCase.Lower ? key.ToLower() : key.ToUpper())];
}
catch(Exception ex)
{
LogUtils.LogError("IgCaseDictionary", Developer.SysDefault, ex, "Key:" + key);
throw ex;
}
finally
{
Locker.ExitReadLock();
}
}
set
{
try
{
Locker.EnterWriteLock();
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("key不能为空![" + key + "]", "key");
}
base[(Case == KeyCase.Lower ? key.ToLower() : key.ToUpper())] = value;
}
finally
{
Locker.ExitWriteLock();
}
}
}
public new bool ContainsKey(string key)
{
try
{
Locker.EnterReadLock();
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("key不能为空![" + key + "]", "key");
}
return base.ContainsKey((Case == KeyCase.Lower ? key.ToLower() : key.ToUpper()));
}
finally
{
Locker.ExitReadLock();
}
}
public new bool Remove(string key)
{
try
{
Locker.EnterWriteLock();
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("key不能为空![" + key + "]", "key");
}
return base.Remove((Case == KeyCase.Lower ? key.ToLower() : key.ToUpper()));
}
finally
{
Locker.ExitWriteLock();
}
}
public new bool TryGetValue(string key, out TValue value)
{
try
{
Locker.EnterReadLock();
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("key不能为空![" + key + "]", "key");
}
return base.TryGetValue((Case == KeyCase.Lower ? key.ToLower() : key.ToUpper()), out value);
}
finally
{
Locker.ExitReadLock();
}
}
public Dictionary Dictionary()
{
return this;
}
}
///
/// 键大小写枚举
///
public enum KeyCase
{
Lower,
Upper
}
}