我有一个静态类来保存字典,2个方法可以访问它
这是我的班级:
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"
如您所见,我的目的是拥有一个静态字典。我认为这个问题出现在字典声明中......但我无法看到......
以防万一,&#34; beDGRAIC&#34;是我的命名空间。
感谢您的帮助!
答案 0 :(得分:1)
您的代码(几乎)对我有用。
我刚刚添加了一个缺少的分号以使其编译,但可以毫无例外地调用ConfiguraCuadros.GetDictionary();
。
以下是缺少分号的代码:
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" },
}
},
}
}
}
}
};
}
[UPDATE]
我同意上面关于检查InnerException作为类型初始化异常的一般规则的注释,特别是关于数据类型的不友好性质的注释!