我正在建立一个序列化系统,它在文件的开头放置一个类型表,以便每个被序列化的字段可以简单地在实际数据之前存储其类型的索引(而不是存储完整的类型名称)与每个领域)。要做到这一点,我需要:
这样的集合是否存在?
答案 0 :(得分:2)
你所描述的已经存在,它被称为......一本字典。你只需要以另一种方式解决问题:索引是你的价值,而不是你的钥匙。
您的方法可能如下所示:
private Dictionary<Type, int> serializationTable = new Dictionary<Type, int>();
public int GetSerializationIndex(Type type)
{
int index;
if (!this.serializationTable.TryGetValue(type, out index))
{
index = this.serializationTable.Count;
this.serializationTable.Add(type, index);
}
return index;
}