public virtual void InitializeFromINIValue(string value) { if (string.IsNullOrWhiteSpace(value)) { return; } GetPropertyInfos(); if (this.Properties.Count == 0) { return; } var kvPair = value.Split(new[] { '=' }, 2); value = kvPair[1].Trim('(', ')', ' '); var pairs = value.Split(DELIMITER); foreach (var pair in pairs) { kvPair = pair.Split('='); if (kvPair.Length != 2) { continue; } var key = kvPair[0].Trim(); var val = kvPair[1].Trim(); var propInfo = this.Properties.FirstOrDefault(p => string.Equals(p.Name, key, StringComparison.OrdinalIgnoreCase)); if (propInfo != null) { StringUtils.SetPropertyValue(val, this, propInfo); } else { propInfo = this.Properties.FirstOrDefault(f => f.GetCustomAttributes(typeof(AggregateIniValueEntryAttribute), false).OfType <AggregateIniValueEntryAttribute>().Any(a => string.Equals(a.Key, key, StringComparison.OrdinalIgnoreCase))); if (propInfo != null) { StringUtils.SetPropertyValue(val, this, propInfo); } } } }
public void Deserialize(object obj) { var iniFiles = new Dictionary <string, IniFile>(); var fields = obj.GetType().GetProperties().Where(f => f.IsDefined(typeof(IniFileEntryAttribute), false)); foreach (var field in fields) { var attributes = field.GetCustomAttributes(typeof(IniFileEntryAttribute), false); foreach (var attr in attributes.OfType <IniFileEntryAttribute>()) { if (attr.Section == IniFileSections.Custom) { // this code is to handle custom sections var collection = field.GetValue(obj) as IIniSectionCollection; if (collection != null) { ReadFile(iniFiles, attr.File); var sectionNames = ReadCustomSectionNames(iniFiles, attr.File); foreach (var sectionName in sectionNames) { var sectionValues = ReadSection(iniFiles, attr.File, sectionName); collection.Add(sectionName, sectionValues); } } } else { var keyName = string.IsNullOrWhiteSpace(attr.Key) ? field.Name : attr.Key; if (attr.WriteBoolValueIfNonEmpty) { // Don't really need to do anything here, we don't care about this on reading it. // extraBoolValue = Convert.ToBoolean(IniReadValue(SectionNames[attr.Section], attr.Key)); } else { var iniValue = ReadValue(iniFiles, attr.File, attr.Section, keyName); var fieldType = field.PropertyType; var collection = field.GetValue(obj) as IIniValuesCollection; if (collection != null) { var section = ReadSection(iniFiles, attr.File, attr.Section); var filteredSection = collection.IsArray ? section.Where(s => s.StartsWith(collection.IniCollectionKey + "[")) : section.Where(s => s.StartsWith(collection.IniCollectionKey + "=")); collection.FromIniValues(filteredSection); } else if (fieldType == typeof(string)) { var stringValue = iniValue; if (attr.Multiline) { stringValue = stringValue.Replace(@"\n", Environment.NewLine); } field.SetValue(obj, stringValue); } else { // Update the ConditionedOn flag, if this field has one. if (!string.IsNullOrWhiteSpace(attr.ConditionedOn)) { var conditionField = obj.GetType().GetProperty(attr.ConditionedOn); if (string.IsNullOrWhiteSpace(iniValue)) { conditionField.SetValue(obj, false); } else { conditionField.SetValue(obj, true); } } if (string.IsNullOrWhiteSpace(iniValue)) { // Skip non-string values which are not found continue; } var valueSet = StringUtils.SetPropertyValue(iniValue, obj, field, attr); if (!valueSet) { throw new ArgumentException($"Unexpected field type {fieldType.ToString()} for INI key {keyName} in section {attr.Section}."); } } } } } } }
protected virtual void FromComplexINIValue(string value) { if (string.IsNullOrWhiteSpace(value)) { return; } GetPropertyInfos(); if (this.Properties.Count == 0) { return; } var kvValue = value.Trim(' '); var propertyValues = SplitCollectionValues(kvValue, DELIMITER); foreach (var property in this.Properties) { var attr = property.GetCustomAttributes(typeof(AggregateIniValueEntryAttribute), false).OfType <AggregateIniValueEntryAttribute>().FirstOrDefault(); var propertyName = string.IsNullOrWhiteSpace(attr?.Key) ? property.Name : attr.Key; var propertyValue = propertyValues.FirstOrDefault(p => p.StartsWith($"{propertyName}=")); if (propertyValue == null) { continue; } var kvPropertyPair = propertyValue.Split(new[] { '=' }, 2); var kvPropertyValue = kvPropertyPair[1].Trim(DELIMITER, ' '); if (attr?.ValueWithinBrackets ?? false) { if (kvPropertyValue.StartsWith("(")) { kvPropertyValue = kvPropertyValue.Substring(1); } if (kvPropertyValue.EndsWith(")")) { kvPropertyValue = kvPropertyValue.Substring(0, kvPropertyValue.Length - 1); } } var collection = property.GetValue(this) as IIniValuesCollection; if (collection != null) { var values = SplitCollectionValues(kvPropertyValue, DELIMITER); values = values.Where(v => !string.IsNullOrWhiteSpace(v)).ToArray(); if (attr?.ListValueWithinBrackets ?? false) { values = values.Select(v => v.Substring(1)).ToArray(); values = values.Select(v => v.Substring(0, v.Length - 1)).ToArray(); } collection.FromIniValues(values); } else { StringUtils.SetPropertyValue(kvPropertyValue, this, property); } } }