我有一个结构可以使用三深嵌套字典很容易地表示,就像这样
private static Dictionary<string, Dictionary<string, Dictionary<string,string>>> PrerenderedTemplates;
Run Code Online (Sandbox Code Playgroud)
结构可能会像这样使用
PrerenderedTemplates[instanceID][templategroup][templatepart]
Run Code Online (Sandbox Code Playgroud)
现在,我意识到这段代码很难阅读,因为通过查看定义语句,你无法分辨出它的用途.我可以真正看到改变它的唯一优势Dictionary<string, PrerenderedTemplate>是可读性.将每个嵌套转换为自己的类(例如class PrerenderedTemplate{} class TemplateGroup{} class TemplatePart{})将为很少(如果有的话)计算优势添加更多代码行.据我所知.
Dictionary在文档/注释中的工作方式更新
所以,受Reza的启发,但无法使用Tuples,我决定创建自己的密钥生成器并实现他的模式:
private Dictionary<string, string> PrerenderedTemplates;
private string GetPrerenderedTemplateKey(string InstanceId, string FeatureId, string OptionId)
{
return new StringBuilder(instanceId)
.Append(FormatTools.LIST_ENTRY_DELIMITER)
.Append(templategroup)
.Append(FormatTools.LIST_ENTRY_DELIMITER)
.Append(templatepart).ToString();
}
Run Code Online (Sandbox Code Playgroud)
FormatTools.LIST_ENTRY_DELIMITERUnicode专用字符在哪里0xe04d.