我有一個靜態類來保存字典和2種get方法來訪問它靜態字典叫「System.TypeInitializationException」
這裏是我的類:
public static class ConfiguraCuadros
{
public static Dictionary<string,Dictionary<string,Dictionary<string,Dictionary<string,string>>>> GetDictionary()
{
// Try to get the result in the static Dictionary
return _configcuadros;
}
public static Dictionary<string, Dictionary<string, Dictionary<string, string>>> GetHoja(string key)
{
// Try to get the result in the static Dictionary
Dictionary<string, Dictionary<string, Dictionary<string, string>>> result = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
if (_configcuadros.TryGetValue(key, out result))
{
return result;
}
else
{
return null;
}
}
public static readonly Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, string>>>> _configcuadros = new Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, string>>>>
{
{ "Formato01", //this is just a hint, the dictionary is much more extensive
new Dictionary<string, Dictionary<string, Dictionary<string, string>>>
{
{
"F01C01A",
new Dictionary<string, Dictionary<string, string>>
{
{
"X",
new Dictionary<string, string>
{
{ "key1" , "value1" },
{ "key2" , "value2" },
{ "key3" , "value3" },
}
},
}
},
}
},
}
}`
當我使用getter方法,
ConfiguraCuadros.GetDictionary();
它拋出一個異常:
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll 'ConfiguraCuadros.GetDictionary()' threw an exception of type 'System.TypeInitializationException' base: {"The type initializer for 'beDGRAIC.ConfiguraCuadros' threw an exception."} TypeName: "beDGRAIC.ConfiguraCuadros"
或
'ConfiguraCuadros.GetHoja("Formato01")' threw an exception of type 'System.TypeInitializationException' base: {"The type initializer for 'beDGRAIC.ConfiguraCuadros' threw an exception."} TypeName: "beDGRAIC.ConfiguraCuadros"
正如你所看到的,我的意思是有一個靜態字典。我認爲問題出在字典聲明中......但我看不到在哪裏......
爲了以防萬一,「beDGRAIC」是我的名字空間。
感謝您的幫助!
閱讀內部異常。 – SLaks 2014-10-29 20:42:37
你可能使用'Tuple'而不是多層嵌套的'Dictionary' – Shiva 2014-10-29 20:43:46
你是認真的'Dictionary>>>'?在修復TypeInitializationException之前,考慮一個好的數據結構。 –
2014-10-29 20:49:02