2014-10-29 92 views
0

我有一個靜態類來保存字典和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」是我的名字空間。

感謝您的幫助!

+3

閱讀內部異常。 – SLaks 2014-10-29 20:42:37

+0

你可能使用'Tuple'而不是多層嵌套的'Dictionary' – Shiva 2014-10-29 20:43:46

+0

你是認真的'Dictionary >>>'?在修復TypeInitializationException之前,考慮一個好的數據結構。 – 2014-10-29 20:49:02

回答

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" }, 
            } 
           }, 
          } 
         } 
        } 
       } 
     }; 
} 

[更新]

我不同意上司的評價上述有關檢查出的InnerException爲一個類型初始化異常的一般規則和特別是關於數據類型的不友好性質!

+0

我在閱讀確切的innerexception時遇到了問題,因爲那個調用是在一個方法中的,只有那個被調用的方法在try-catch方括號中,是的,答案在innerexception中。 – culebrin 2014-10-29 21:42:47

+0

現在我有一個奇怪的問題,在一個字典中使用雙引號字符串'{「TXT」,「(3 /)」「A」「。」 },'',編譯器強調'「A」「。」',我不知道爲什麼......(它表示''期望') – culebrin 2014-10-29 23:20:34

+0

編譯器(或我!)不清楚你的字符串是什麼。如果將字符包含在字符串中,則需要轉義字符。 – 2014-10-30 00:38:13