1. Vlad-00003

    Vlad-00003 Shack Builder Plugin Developer

    Hello everyone!
    I've run into a strange problem - I can't proparly save and ready dictionaries and my own custom classes form the Config.
    Here is what I'm trying to do:
    I have something like this:

    Code (Text):

            private Dictionary<string, string> dict1;
            private Dictionary<string, string> dict2;
    And what I wanted is to save it to the config and read it back.
    This is the code I'm using to get data in\out of config:
    Code (Text):
    private void GetConfig<T>(string Key, ref T var)

            {
                if (Config[Key] != null)
                {
                    var = (T)Convert.ChangeType(Config[Key], typeof(T));
                }
                Config[Key] = var;
            }
    Fill dicts with something and trying to get it to config, and I expect it to work:
    Code (Text):

    private void LoadConfigValues()
    {
         dict1 = new Dictionary<string, string>()
         {
              ["Test1"] = "test1",
              ["Test2"] = "test2"    
         }
         dict2 = new Dictionary<string, string>()
         {
              ["Test3"] = "test3",
              ["Test4"] = "test4"
         }
         GetConfig("Dictionary 1", ref dict1);
         Getconfig("Dictionary 2", ref dict2);
         SaveConfig();
    }
     
    But here comes an error-
    Code (Text):
    Failed to initialize plugin 'test v1.0.0' (InvalidCastException: Value is not a convertible object: System.Collections.Generic.Dictionary`2[System.String,System.Object] to System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]])
    So the problem is - Config[Key] returns me not Dictionary<string,string>, but Dictionary<string,object>
    How can I avoid it?

    The only solution I've found so far is doing something creepy like that:
    Code (Text):

    private Dictionary<string, string> Dict1;
    private Dictionary<string, string> Dict2;

    private void LoadConfigValues()
    {
         Dictionary<string, object> dict1 = new Dictionary<string, object>()
         {
              ["Test1"] = "test1",
              ["Test2"] = "test2"    
         }
         Dictionary<string, object> dict2 = new Dictionary<string, object>()
         {
              ["Test3"] = "test3",
              ["Test4"] = "test4"
         }
         GetConfig("Dictionary 1", ref dict1);
         Getconfig("Dictionary 2", ref dict2);
         foreach(var item in dict1)
         {
              Dict1.Add(item.Key, item.Value.ToString());
          }
         foreach(var item in dict2)
         {
              Dict2.Add(item.Key, item.Value.ToString());
         }
         SaveConfig();

    }
     
    And that's creepy.



    About classes - that's the over question.
    Let's say I have a class:
    Code (Text):
           class DnN
            {
                public Dictionary<string, object> Day { get; set; }
                public Dictionary<string, object> Night { get; set; }
                public DnN()
                {
                    Day = new Dictionary<string, object>(); //Same problem as above, so this time I decided to just
                    Night = new Dictionary<string, object>(); // Change Dictionaty<string,float> to <string,object>
                }
            }
    Is there any way to store it?
    --- Double Post Merged, Apr 14, 2017 ---
    Also - if anyone would be able to tell me why this code works:
    Code (Text):
    private void GetConfig<T>(string Key, ref T var)
            {
                if (Config[Key] != null)
                {
                    var = (T)Convert.ChangeType(Config[Key], typeof(T));
                }
                Config[Key] = var;
            }
    While this(just to be able to save config into the sub directories) gives me cast error:

    Code (Text):
            private void GetConfig<T>(ref T value, params object[] args)
            {
                List<string> stringArgs = (from arg in args select arg.ToString()).ToList<string>();
                if (Config.Get(stringArgs.ToArray()) != null)
                {
                    value = (T)Convert.ChangeType(Config.Get(stringArgs.ToArray()), typeof(T));
                }
                Config.Set(args, value);
            }