public void LoadFromXml(XmlNode node) { DirectValues.Clear(); var settingNodes = node.SelectNodes(@"setting"); if (settingNodes != null) { foreach (XmlNode settingNode in settingNodes) { if (settingNode.Attributes != null) { string name; XmlHelper.ReadAttribute(out name, settingNode.Attributes[@"name"]); var value = settingNode.SelectSingleNode(@"value"); if (value != null) { DirectValues[name] = value.InnerText; } else { DirectValues[name] = null; } } } } }
void AllValuesAreDirectValues() { if (!DirectValues.All(dv => dv.Value.ObjectType == ObjectTypes.DirectValue)) { throw new Exception("some fields are not direct values"); } }
public void LoadFromXml(XmlNode node) { DirectValues.Clear(); var settingNodes = node.SelectNodes(@"setting"); if (settingNodes != null) { foreach (XmlNode settingNode in settingNodes) { if (settingNode.Attributes != null) { XmlHelper.ReadAttribute(out string name, settingNode.Attributes[@"name"]);
public void Extract(object instance) { if (instance == null) { return; } IList <PropertyInfo> props = new List <PropertyInfo>(instance.GetType().GetProperties()); foreach (PropertyInfo prop in props) { var t = prop.PropertyType; // direct value if (t.IsPrimitive || t.IsValueType || (t == typeof(string))) { DirectValues.Add(prop.Name, new ObjectWrapper { ObjectType = ObjectTypes.DirectValue, Value = prop.GetValue(instance) }); } else if (t.GetInterface("ICollection") != null) { var collection = prop.GetValue(instance) as ICollection; //bool isValid = collection.Select(x => ((dynamic)x).GetType()).Distinct().Count() == 1; //if ( !isValid ) continue; var objectWrapper = new ObjectWrapper { ObjectType = ObjectTypes.ObjectList, Value = new List <object>() }; var ve = new ValueExtractor(); foreach (var item in collection) { (objectWrapper.Value as List <object>).Add(item); } ObjectLists.Add(prop.Name, objectWrapper); } else if (t.GetInterface("IEnumerable") != null) { var enumerable = prop.GetValue(instance) as IEnumerable <object>; var isValid = enumerable.Select(x => x.GetType()).Distinct().Count() == 1; if (!isValid) { continue; } Console.WriteLine(prop.Name); //var ve = new ValueExtractor(); //foreach ( var item in enumerable ) //{ // var xs = new ValueExtractor(); // xs.Extract(prop.GetValue(item)); // Objects.Add(prop.Name, new ObjectWrapper // { // ObjectType = ObjectTypes.Object, // Value = xs.DirectValues // }); //} } // object else { var xs = new ValueExtractor(); xs.Extract(prop.GetValue(instance)); Objects.Add(prop.Name, new ObjectWrapper { ObjectType = ObjectTypes.Object, Value = xs.DirectValues }); } } }