public void Deserialize(object graph) { var fieldInfos = graph.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic); internalsLocked = false; foreach (var fieldInfo in fieldInfos) { // Field has no CfgSettings attached, so ignore it if (!Attribute.IsDefined(fieldInfo, typeof(CfgSettingAttribute))) { continue; } CfgSettingAttribute attr = Attribute.GetCustomAttribute(fieldInfo, typeof(CfgSettingAttribute)) as CfgSettingAttribute; if (!readOnlyValues.Contains(attr.ToString())) { readOnlyValues.Add(attr.ToString()); } try { if (fieldInfo.FieldType.IsPrimitive || fieldInfo.FieldType.Namespace == "System") { fieldInfo.SetValue(graph, Convert.ChangeType(this.getValue(attr.Section, attr.Entry, false, attr.DefaultValue, true), fieldInfo.FieldType, CultureInfo.InvariantCulture)); } else { object oVal = fieldInfo.GetValue(graph); if (oVal is IList) { List <string> s = new List <string>(this.getValue(attr.Section, attr.Entry, false, attr.DefaultValue, true).Split(',')); fieldInfo.SetValue(graph, Convert.ChangeType(s, fieldInfo.FieldType, CultureInfo.InvariantCulture)); } } } catch (Exception ex) { logger.Error("Error deserializing {0} line {1} : {2} ", fileName, lastLine, ex.ToString()); throw new FormatException(String.Format("Error parsing {0} line {1}", fileName, lastLine)); } } internalsLocked = true; }
public void Serialize(object graph) { var fieldInfos = graph.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); internalsLocked = false; foreach (var fieldInfo in fieldInfos) { // Field has no CfgSettings attached, so ignore it if (!Attribute.IsDefined(fieldInfo, typeof(CfgSettingAttribute))) { continue; } CfgSettingAttribute attr = Attribute.GetCustomAttribute(fieldInfo, typeof(CfgSettingAttribute)) as CfgSettingAttribute; if (!readOnlyValues.Contains(attr.ToString())) { readOnlyValues.Add(attr.ToString()); } if (fieldInfo.FieldType.IsPrimitive || fieldInfo.FieldType.Namespace == "System") { this.setValue(attr.Section, attr.Entry, Convert.ToString(fieldInfo.GetValue(graph), CultureInfo.InvariantCulture), false); } else { object oVal = fieldInfo.GetValue(graph); if (oVal is IList) { IList l = oVal as IList; string s = String.Empty; if (l.Count > 0) { s = Convert.ToString(l[0], CultureInfo.InvariantCulture); for (int i = 1; i < l.Count; i++) { s += "," + Convert.ToString(l[i], CultureInfo.InvariantCulture); } } this.setValue(attr.Section, attr.Entry, s, false); } } } internalsLocked = true; }