예제 #1
0
        void LoadSettingsFromClass(ConfigFile configFile, Type settingsClass, string sectionName)
        {
            foreach (FieldInfo fieldInfo in settingsClass.GetFields())
            {
                if (fieldInfo.FieldType == typeof(Dictionary<string, string>))
                {
                    Dictionary<string, string> dict = new Dictionary<string, string>();
                    var section = configFile.GetSection(sectionName);

                    foreach (var property in section.GetProperties())
                    {
                        var value = section.GetValue(property, "");
                        if (dict.ContainsKey(property))
                        {
                            dict.Add(property, value);
                        }
                        else
                        {
                            dict[property] = value;
                        }
                    }

                    fieldInfo.SetValue(null, dict);
                }
                else if (configFile.HasProperty(sectionName, fieldInfo.Name))
                {
                    string stringValue = configFile.GetValue(sectionName, fieldInfo.Name, "");
                    object value = StringConverter.Parse(stringValue, fieldInfo.FieldType);

                    fieldInfo.SetValue(null, value);
                }
            }

            foreach (Type nestedType in settingsClass.GetNestedTypes())
            {
                string newSectionName = sectionName;
                if (newSectionName.Length > 0)
                {
                    newSectionName += ".";
                }
                newSectionName += nestedType.Name;

                LoadSettingsFromClass(configFile, nestedType, newSectionName);
            }
        }