public void SetProperty(string path, object value) { var tokens = path.Split('.'); var parentNames = tokens.Take(tokens.Length - 1); var targetName = tokens.Last(); IList nodes = this; foreach (var parentName in parentNames) { var parent = nodes .OfType <Property>() .FirstOrDefault(x => x.Name.EqualsIgnoreCase(parentName)); if (parent == null) { parent = new Property(parentName); nodes.Add(parent); } nodes = parent.Value as VObject; if (nodes == null) { nodes = new VObject(); parent.Value = (VObject)nodes; } } nodes.Add(new Property(targetName, value)); }
public VObject Clone() { var collection = new VObject(); collection.AddMany(this.Select(x => x.Clone())); return(collection); }
public static VObject CreateObject(object source) { IEnumerable <Tuple <string, PropertyInfo> > properties; if (source._HasAttribute <DataContractAttribute>()) { properties = from property in source.GetType().GetProperties() let attribute = property._GetAttribute <DataMemberAttribute>() where attribute != null orderby attribute.Order select Tuple.Create(attribute.Name ?? property.Name, property); } else if (source._HasAttribute <MediaContractAttribute>()) { properties = from property in source.GetType().GetProperties() let attribute = property._GetAttribute <MediaMemberAttribute>() where attribute != null orderby attribute.Order select Tuple.Create(attribute.Name ?? property.Name, property); } else { properties = from property in source.GetType().GetProperties() where !property._HasAttribute <XmlIgnoreAttribute>() let attribute = property._GetAttribute <XmlElementAttribute>() orderby attribute?.Order ?? 0 select Tuple.Create(attribute?.ElementName ?? property.Name, property); } var @object = new VObject(); foreach (var property in properties) { var targetProperty = property.Item1; var sourceValueGetter = property.Item2; var sourceValue = sourceValueGetter.GetValue(source); var targetValue = Create(sourceValue); @object.Add(new Property(targetProperty, targetValue)); } return(@object); }