private void SetToSection(IniSection section, Setting s, bool buildComment) { foreach (var propInfo in s.GetType().GetProperties()) { if (propInfo.Name == "Id") continue; if (propInfo.PropertyType.IsArray) { var ary = (Array)propInfo.GetValue(s); if (ary == null) continue; IList<string> aryJoin = (from object ele in ary select Convert.ToString(ele)).ToList(); section.Set(propInfo.Name, string.Join(",", aryJoin.ToArray())); continue; } string comment = null; if (buildComment) { var descript = propInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (descript.Length > 0) { comment = ((DescriptionAttribute)descript[0]).Description.Replace("\r\n", "#\r\n"); } } section.Set(propInfo.Name, Convert.ToString(propInfo.GetValue(s)), comment); } }
private void Load(TextReader reader) { IniSection section = null; string line; while ((line = reader.ReadLine()) != null) { line = line.Trim(); // skip empty lines if (line == string.Empty) continue; // skip comments if (line.StartsWith(";") || line.StartsWith("#")) continue; if (line.StartsWith("[") && line.EndsWith("]")) { var sectionName = line.Substring(1, line.Length - 2); if (!_sections.ContainsKey(sectionName)) { section = new IniSection(sectionName); _sections.Add(sectionName, section); } continue; } if (section != null) { var keyValue = line.Split(new[] {"="}, 2, StringSplitOptions.RemoveEmptyEntries); if (keyValue.Length != 2) continue; section.Set(keyValue[0].Trim(), keyValue[1].Trim()); } } }
private void FillProperities(Setting setting, IniSection section) { var iniProperties = section.Properties.ToDictionary(s => s.Name, s => s.Value); foreach (var propertyInfo in setting.GetType().GetProperties()) { if (propertyInfo.Name == "Id") continue; if (iniProperties.ContainsKey(propertyInfo.Name)) { var strValue = iniProperties[propertyInfo.Name]; if (propertyInfo.PropertyType == typeof(string)) { propertyInfo.SetValue(setting, strValue); continue; } if (propertyInfo.PropertyType.IsEnum) { propertyInfo.SetValue(setting, Enum.Parse(propertyInfo.PropertyType, strValue)); continue; } if (propertyInfo.PropertyType == typeof(int)) { propertyInfo.SetValue(setting, Convert.ToInt32(strValue)); continue; } if (propertyInfo.PropertyType == typeof(bool)) { propertyInfo.SetValue(setting, Convert.ToBoolean(strValue)); continue; } if (propertyInfo.PropertyType.IsArray && propertyInfo.PropertyType.GetElementType() == typeof(string)) { var aryStrValue = strValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); propertyInfo.SetValue(setting, aryStrValue); continue; } throw new ApplicationException(string.Format("Can't Convert {0} to type {1}", strValue, propertyInfo.PropertyType.Name)); } } }
/// <summary> /// Get a section by name. If the section doesn't exist, it is created. /// </summary> /// <param name="sectionName">The name of the section.</param> /// <returns>A section. If the section doesn't exist, it is created.</returns> public IniSection Section(string sectionName) { IniSection section; if (!_sections.TryGetValue(sectionName, out section)) { section = new IniSection(sectionName); _sections.Add(sectionName, section); } return section; }