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.
因此对字典的调用将具有通用值类型:
So calls to dictionary will have a generic value type:
用法很简单:
对于迭代部分
针对字典键的访问者模式可以解决问题.
A visitor pattern against the dictionary keys will do the trick.
首先是访问者界面:
然后我们让HeterogeneousDictionaryKey
配合:
现在我们可以公开 HeterogeneousDictionary
键:
Now we can expose the HeterogeneousDictionary
keys:
就是这样.
这里是一个安全地将字典复制到另一个字典的用法示例
Here an example of usage to safely copy a dictionary to an other
这篇关于遍历异构和类型安全的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!