Search code examples
c#.netdictionarystatic-classesstatic-class

Static dictionary call "System.TypeInitializationException"


I have a static class to hold a dictionary and 2 get methods to access it

Here is my class:


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

                }

            },
        }
    }`

When I use the getter method,

ConfiguraCuadros.GetDictionary();

It throws an exception:

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"

or

'ConfiguraCuadros.GetHoja("Formato01")' threw an exception of type 'System.TypeInitializationException'
    base: {"The type initializer for 'beDGRAIC.ConfiguraCuadros' threw an exception."}
    TypeName: "beDGRAIC.ConfiguraCuadros"

As you can see, my intention is to have a static dictionary. I think the problem is in the dictionary declaration ... but I can't see where...

Just in case, "beDGRAIC" is my namespace.

Thanks for your help!


Solution

  • Your code works (almost) as is for me.

    I just added a missing semi-colon to get it to compile but can then call ConfiguraCuadros.GetDictionary(); without any exception.

    Here is the code back with that missing semicolon:

    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]

    I do agree with the comments above about checking out the InnerException as a general rule for a type initialisation exception and, particularly, about the unfriendly nature of the datatype!