我想在解析大型XML文件后创建一个嵌套的ConcurrentDictionary。 运行下面的代码时得到的是最里面的(tmpDictionaryDid)字典为null。
我想我没有正确保存词典或加载错误,但我无法弄清楚哪些是不正确的。
甚至可以使用ConcurrentDictionary解决它,还是应该尝试使用其他解决方案?
public void temp22db()
{
ConcurrentDictionary<string, ConcurrentDictionary<string, string>> dictionary22 = new ConcurrentDictionary<string, ConcurrentDictionary<string, string>>();
try
{
ConcurrentDictionary<string, string> did = new ConcurrentDictionary<string, string>();
did.TryAdd("F186 1", "test1");
did.TryAdd("F186 2", "test2");
dictionary22.TryAdd("F186", did);
did.TryAdd("EDA0 1", "test3");
did.TryAdd("EDA0 2", "test4");
dictionary22.TryAdd("EDA0", did);
string test;
did.TryGetValue("EDA0 1", out test);
Helper.logInWindow("This works " + test);
}
catch (Exception e)
{
Helper.logInWindow("SDDBTemp: " + e.Message);
}
ecuDictionary2.TryAdd("1638", dictionary22);
}
public string getDIDInterpretation()
{
string outStr = "";
ConcurrentDictionary<string, ConcurrentDictionary<string, string>> tmpDictionary;
ecuDictionary2.TryGetValue("1638", out tmpDictionary);
if (tmpDictionary != null)
{
ConcurrentDictionary<string, string> tmpDictionaryDid;
tmpDictionary.TryGetValue("EDA0", out tmpDictionaryDid);
if (tmpDictionaryDid != null)
{
tmpDictionaryDid.TryGetValue("EDA0 1", out outStr);
}
}
Helper.logInWindow("Why U no work? " + outStr);
return outStr;
}
答案 0 :(得分:0)
你应该这样使用它:(父ConcurrentDictionary将允许你的内部词典的并发)
public static void temp22db()
{
Dictionary<string, Dictionary<string, string>> dictionary22 = new Dictionary<string, Dictionary<string, string>>();
try
{
Dictionary<string, string> did = new Dictionary<string, string>();
did.Add("F186 1", "test1");
did.Add("F186 2", "test2");
dictionary22.Add("F186", did);
did.Add("EDA0 1", "test3");
did.Add("EDA0 2", "test4");
dictionary22.Add("EDA0", did);
string test;
did.TryGetValue("EDA0 1", out test);
Helper.logInWindow("This works " + test);
}
catch (Exception e)
{
Helper.logInWindow("SDDBTemp: " + e.Message);
}
ecuDictionary2.TryAdd("1638", dictionary22);
}
public static string getDIDInterpretation()
{
string outStr = "";
Dictionary<string, Dictionary<string, string>> tmpDictionary;
ecuDictionary2.TryGetValue("1638", out tmpDictionary);
if (tmpDictionary != null)
{
Dictionary<string, string> tmpDictionaryDid;
tmpDictionary.TryGetValue("EDA0", out tmpDictionaryDid);
if (tmpDictionaryDid != null)
{
tmpDictionaryDid.TryGetValue("EDA0 1", out outStr);
}
}
Helper.logInWindow("Why U no work? " + outStr);
return outStr;
}