Iterate trough an heterogeneous and type-safe dictionary(遍历异构和类型安全的字典)
问题描述
我需要一个像 字典 但是数据类型(TValue)从一个键变为另一个键.
I need a container that works like a ditionary but where the type of data (TValue) change from one key to the other.
我还需要遍历它.
推荐答案
对于异构和类型安全的字典部分
Wilka 响应是一个好的开始.
诀窍是将类型放入键中.
The trick is to put the type in the key.
/// <summary>
/// Base class for all dictionary key.
///
/// <remarks>The key name is REALLY usefull for debug purpose.</remarks>
/// </summary>
abstract class HeterogeneousDictionaryKeyBase
{
readonly string _name;
protected HeterogeneousDictionaryKeyBase(string name)
{
_name = name;
}
public override string ToString()
{
return _name;
}
}
sealed class HeterogeneousDictionaryKey<TValue> : HeterogeneousDictionaryKeyBase
{
public HeterogeneousDictionaryKey(string name)
: base(name)
{
}
}
因此对字典的调用将具有通用值类型:
So calls to dictionary will have a generic value type:
/// <summary>
/// <remarks>The [] operator can not be generic, so we implement it has a getter and a setter</remarks>
/// </summary>
class HeterogeneousDictionary
{
private readonly Dictionary<HeterogeneousDictionaryKeyBase, object> _dictionary = new Dictionary<HeterogeneousDictionaryKeyBase, object>();
public void Add<TValue>(HeterogeneousDictionaryKey<TValue> key, TValue value)
{
_dictionary.Add(key, value);
}
public TValue Get<TValue>(HeterogeneousDictionaryKey<TValue> key)
{
return (TValue)_dictionary[key];
}
public void Set<TValue>(HeterogeneousDictionaryKey<TValue> key, TValue value)
{
_dictionary[key] = value;
}
public bool TryGetValue<TValue>(HeterogeneousDictionaryKey<TValue> key, out TValue value)
{
object result;
if (_dictionary.TryGetValue(key, out result) && result is TValue)
{
value = (TValue)result;
return true;
}
value = default(TValue);
return false;
}
}
用法很简单:
var dictionary = new HeterogeneousDictionary();
var keyName = new HeterogeneousDictionaryKey<string>("keyName");
var keyAge = new HeterogeneousDictionaryKey<int>("keyAge");
dictionary.Set(keyName, "Orace");
dictionary.Set(keyAge, 8);
...
var name = dictionary.Get(keyName);
var age = dictionary.Get(keyAge);
对于迭代部分
针对字典键的访问者模式可以解决问题.
A visitor pattern against the dictionary keys will do the trick.
首先是访问者界面:
interface IHeterogeneousDictionaryKeyVisitor
{
void Visit<TValue>(HeterogeneousDictionaryKey<TValue> key);
}
然后我们让HeterogeneousDictionaryKey配合:
abstract class HeterogeneousDictionaryKeyBase
{
...
public abstract void Accept(IHeterogeneousDictionaryKeyVisitor visitor);
...
}
sealed class HeterogeneousDictionaryKey<TValue> : HeterogeneousDictionaryKeyBase
{
...
public override void Accept(IHeterogeneousDictionaryKeyVisitor visitor)
{
visitor.Visit(this);
}
}
现在我们可以公开 HeterogeneousDictionary 键:
Now we can expose the HeterogeneousDictionary keys:
class HeterogeneousDictionary
{
...
public Dictionary<HeterogeneousDictionaryKeyBase, object>.KeyCollection Keys
{
get { return _dictionary.Keys; }
}
...
}
就是这样.
这里是一个安全地将字典复制到另一个字典的用法示例
Here an example of usage to safely copy a dictionary to an other
class DictionaryCopier : IHeterogeneousDictionaryKeyVisitor
{
readonly HeterogeneousDictionary _source;
readonly HeterogeneousDictionary _destination;
public DictionaryCopier(HeterogeneousDictionary source, HeterogeneousDictionary destination)
{
_source = source;
_destination = destination;
}
public void PerformCopy()
{
foreach (var key in _source.Keys)
{
// See you soon.
key.Accept(this);
}
}
/// <summary>
/// We fall back here with a typed key.
/// </summary>
public void Visit<TValue>(HeterogeneousDictionaryKey<TValue> key)
{
// Here the value is typed.
var value = _source.Get(key);
_destination.Add(key, value);
}
}
这篇关于遍历异构和类型安全的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:遍历异构和类型安全的字典
基础教程推荐
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
