我有同样的情况,并创建了一个 WrapperDictionary 类和一个强制转换扩展方法。如果您还想对它进行操作(例如删除项目),会发现它很有帮助。
例子:
const string TestKey1 = "Key";
const string TestValue1 = "Value";
var dict = new Dictionary<string, string>();
dict.Add(TestKey1, TestValue1);
var wrapper = dict.CastDictionary<string, string, string, object>();
wrapper.Remove(TestKey1);
代码:
public class DictionaryWrapper<TKeyTarget, TValueTarget, TKeySource, TValueSource> : IDictionary<TKeyTarget, TValueTarget>
{
#region types
private class EnumeratorWrapper : IEnumerator<KeyValuePair<TKeyTarget, TValueTarget>>
{
private readonly IEnumerator<KeyValuePair<TKeySource, TValueSource>> _enumerator;
private readonly DictionaryWrapper<TKeyTarget, TValueTarget, TKeySource, TValueSource> _dictionaryWrapper;
public EnumeratorWrapper(IEnumerator<KeyValuePair<TKeySource, TValueSource>> enumerator, DictionaryWrapper<TKeyTarget, TValueTarget, TKeySource, TValueSource> dictionaryWrapper)
{
_enumerator = enumerator;
_dictionaryWrapper = dictionaryWrapper;
}
public void Dispose()
{
_enumerator.Dispose();
}
public bool MoveNext()
{
return _enumerator.MoveNext();
}
public void Reset()
{
_enumerator.Reset();
}
public KeyValuePair<TKeyTarget, TValueTarget> Current => _dictionaryWrapper._kvpSourceToTargetFunc(_enumerator.Current);
object IEnumerator.Current => Current;
}
#endregion
#region fields
private readonly IDictionary<TKeySource, TValueSource> _dictionary;
private readonly Func<TKeySource, TKeyTarget> _keySourceToTargetFunc;
private readonly Func<TKeyTarget, TKeySource> _keyTargetToSourceFunc;
private readonly Func<TValueSource, TValueTarget> _valueSourceToTargetFunc;
private readonly Func<TValueTarget, TValueSource> _valueTargetToSourceFunc;
private readonly Func<KeyValuePair<TKeySource, TValueSource>, KeyValuePair<TKeyTarget, TValueTarget>> _kvpSourceToTargetFunc;
private readonly Func<KeyValuePair<TKeyTarget, TValueTarget>, KeyValuePair<TKeySource, TValueSource>> _kvpTargetToSourceFunc;
#endregion
#region Construction
public DictionaryWrapper(
IDictionary<TKeySource, TValueSource> dict,
Func<TKeySource, TKeyTarget> keySourceToTargetFunc = null,
Func<TKeyTarget, TKeySource> keyTargetToSourceFunc = null,
Func<TValueSource, TValueTarget> valueSourceToTargetFunc = null,
Func<TValueTarget, TValueSource> valueTargetToSourceFunc = null)
{
_dictionary = dict;
_keySourceToTargetFunc = keySourceToTargetFunc ?? (i => (TKeyTarget) (object) i);
_keyTargetToSourceFunc = keyTargetToSourceFunc ?? (i => (TKeySource) (object) i);
_valueSourceToTargetFunc = valueSourceToTargetFunc ?? (i => (TValueTarget) (object) i);
_valueTargetToSourceFunc = valueTargetToSourceFunc ?? (i => (TValueSource) (object) i);
_kvpSourceToTargetFunc =
kvp => new KeyValuePair<TKeyTarget, TValueTarget>(_keySourceToTargetFunc(kvp.Key), _valueSourceToTargetFunc(kvp.Value));
_kvpTargetToSourceFunc =
kvp => new KeyValuePair<TKeySource, TValueSource>(_keyTargetToSourceFunc(kvp.Key), _valueTargetToSourceFunc(kvp.Value));
}
#endregion
#region Interface Members
public IEnumerator<KeyValuePair<TKeyTarget, TValueTarget>> GetEnumerator()
{
return new EnumeratorWrapper(_dictionary.GetEnumerator(), this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(KeyValuePair<TKeyTarget, TValueTarget> item)
{
_dictionary.Add(_kvpTargetToSourceFunc(item));
}
public void Clear()
{
_dictionary.Clear();
}
public bool Contains(KeyValuePair<TKeyTarget, TValueTarget> item)
{
return _dictionary.Contains(_kvpTargetToSourceFunc(item));
}
public void CopyTo(KeyValuePair<TKeyTarget, TValueTarget>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public bool Remove(KeyValuePair<TKeyTarget, TValueTarget> item)
{
return _dictionary.Remove(_kvpTargetToSourceFunc(item));
}
public int Count => _dictionary.Count;
public bool IsReadOnly => _dictionary.IsReadOnly;
public bool ContainsKey(TKeyTarget key)
{
return _dictionary.ContainsKey(_keyTargetToSourceFunc(key));
}
public void Add(TKeyTarget key, TValueTarget value)
{
_dictionary.Add(_keyTargetToSourceFunc(key), _valueTargetToSourceFunc(value));
}
public bool Remove(TKeyTarget key)
{
return _dictionary.Remove(_keyTargetToSourceFunc(key));
}
public bool TryGetValue(TKeyTarget key, out TValueTarget value)
{
var success = _dictionary.TryGetValue(_keyTargetToSourceFunc(key), out TValueSource result);
value = success ? _valueSourceToTargetFunc(result) : default;
return success;
}
public TValueTarget this[TKeyTarget key]
{
get => _valueSourceToTargetFunc(_dictionary[_keyTargetToSourceFunc(key)]);
set => _dictionary[_keyTargetToSourceFunc(key)] = _valueTargetToSourceFunc(value);
}
public ICollection<TKeyTarget> Keys => _dictionary.Keys.Select(k => _keySourceToTargetFunc(k)).ToList();
public ICollection<TValueTarget> Values => _dictionary.Values.Select(v => _valueSourceToTargetFunc(v)).ToList();
#endregion
}
public static class DictionaryWrapperExtensions
{
public static IDictionary<TKeyCasted, TValueCasted> CastDictionary<TKey, TValue, TKeyCasted, TValueCasted>(this IDictionary<TKey, TValue> dictionary)
{
return new DictionaryWrapper<TKeyCasted,TValueCasted,TKey,TValue>(dictionary);
}
}