// Saving --------------------------------------------------------------------- /// <summary>Saves the values to the ini document using reflection.</summary> private void SaveValues(IniDocument document) { Type type = this.GetType(); IniSection globalSection = document.GlobalSection; globalSection.Comments = HeaderComments; var properties = type.GetProperties( BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo propertyInfo in properties) { var isSection = propertyInfo.GetCustomAttribute <SectionAttribute>(); if (isSection != null) { IniSection section = SaveSection(propertyInfo, propertyInfo.GetValue(this)); if (section.Any()) { document.Add(section); } } else if (propertyInfo.IsBrowsable()) { IniProperty property = SaveProperty(propertyInfo, this); globalSection.Add(property); } } }
/// <summary>Writes the property to a string.</summary> private string WriteProperty(IniProperty property) { string text = ""; if (property.Comments.Any()) { text += FormatComments(property.Comments); } text += FormatProperty(property.Name, property.Value, property.UseQuotes) + "\n"; return(text); }
/// <summary>Loads the value from the ini property using reflection.</summary> private static bool LoadProperty(IniProperty property, PropertyInfo propertyInfo, object instance) { object value = null; if (propertyInfo.PropertyType.IsEnum) { string text = property.GetString(); try { value = Enum.Parse(propertyInfo.PropertyType, text); } catch { return(false); } } else if (PropertyIs <string>(propertyInfo)) { value = property.GetString(); } else if (PropertyIs <bool>(propertyInfo)) { bool tryValue; if (!property.TryGetBool(out tryValue)) { return(false); } value = tryValue; } else if (PropertyIs <int>(propertyInfo)) { int tryValue; if (!property.TryGetInt(out tryValue)) { return(false); } value = tryValue; } else if (PropertyIs <float>(propertyInfo)) { float tryValue; if (!property.TryGetFloat(out tryValue)) { return(false); } value = tryValue; } else { return(false); } propertyInfo.SetValue(instance, value); return(true); }