private void SetProperty(object graph, string property, NodeModel document) { object value = null; bool customValue = false; var propertyInfo = graph._GetPropertyInfo(property); property = propertyInfo?.Name ?? property.ChangeCase(TextCase.PascalCase); if (graph is IPropertyValueSetter setter) { var done = setter.SetProperty(property, document, this); if (done) { return; } } if (graph is IPropertyValueFactory factory) { customValue = factory.CreateValue(property, document, this, out value); if (!customValue && value != null) { value = null; } } if (Is.Dictionary(graph)) { var keyType = TypeOf.DictionaryKey(graph); var key = Change.To(property, keyType); if (!customValue) { var valueType = TypeOf.DictionaryValue(graph); value = CreateGraph(document, valueType); } var dictionary = (IDictionary)graph; dictionary.Add(key, value); return; } if (propertyInfo == null) { throw new NoSuchPropertyException(graph.GetType(), property); } if (!customValue) { value = CreateGraph(document, propertyInfo.PropertyType); } graph._Set(property, value); }
public void SetValue(object value) { if (Bag != null) { Bag.Add(value); return; } if ((value is Bag bag) && (Host is IDictionary map)) { var type = TypeOf.DictionaryValue(map); if (type == typeof(object)) { var attrs = Host._GetAttributes <KnownTypeAttribute>(); type = ( from attr in attrs where typeof(ICollection).IsAssignableFrom(attr.Type) && !typeof(IDictionary).IsAssignableFrom(attr.Type) select attr.Type ).FirstOrDefault(); if (type == null) { throw new NotSupportedException("Tipo de mapeamento não suportado: " + Host.GetType().FullName); } } value = Change.To(value, type); } var realName = properties.Last(); if (adders != null) { object key, val; foreach (var adder in adders) { var keyType = adder.GetParameters().First().ParameterType; var valType = adder.GetParameters().Last().ParameterType; if (Change.Try(realName, keyType, out key) && Change.Try(value, valType, out val)) { Host._Call("Add", key, val); return; } } } var propName = ResolvePropertyName(Host, realName); Host._Set(propName, value); }
public object NewValue() { if (Bag != null) { return(Activator.CreateInstance(Bag.ElementType)); } else if (Host is IDictionary map) { var type = TypeOf.DictionaryValue(map); if (type == typeof(object)) { var attrs = Host._GetAttributes <KnownTypeAttribute>(); type = ( from attr in attrs where typeof(IDictionary).IsAssignableFrom(attr.Type) select attr.Type ).FirstOrDefault(); if (type == null) { type = typeof(HashMap); } } var instance = Activator.CreateInstance(type); var property = properties.LastOrDefault(); map[property] = instance; return(instance); } else { var property = properties.LastOrDefault(); var propertyName = ResolvePropertyName(Host, property); var propertyInfo = Host._GetPropertyInfo(propertyName); var propertyType = propertyInfo.PropertyType; if (propertyType == typeof(object)) { propertyType = typeof(HashMap); } var instance = Activator.CreateInstance(propertyType); return(instance); } }
public T CopyProperties <T>(HashMap properties, T graph) { foreach (var entry in properties) { var key = entry.Key; var value = entry.Value; if (Is.Dictionary(graph)) { var map = (IDictionary)graph; var type = TypeOf.DictionaryValue(graph); value = Change.To(value, type); map[key] = value; } else { var info = graph._GetPropertyInfo(key); var type = info?.PropertyType; if (type == null) { var name = graph.GetType().FullName.Split(',').First(); throw new SerializationException( $"A propriedade não existe no objeto destino: {name}.{key}" ); } if (value is HashMap map && !type.IsAssignableFrom(value.GetType())) { value = Activator.CreateInstance(type); CopyProperties(map, value); } graph._Set(key, value); } } return(graph); }
public object NewValue() { if (Bag != null) { var value = Activator.CreateInstance(Bag.ElementType); Bag.Add(value); return(value); } else if (Host is IDictionary map) { var type = TypeOf.DictionaryValue(map); if (type == typeof(object)) { var attrs = Host._GetAttributes <KnownTypeAttribute>(); type = ( from attr in attrs where typeof(IDictionary).IsAssignableFrom(attr.Type) select attr.Type ).FirstOrDefault(); if (type == null) { throw new NotSupportedException("Tipo de mapeamento não suportado: " + Host.GetType().FullName); } } var instance = Activator.CreateInstance(type); var property = properties.LastOrDefault(); map[property] = instance; return(instance); } else { var property = properties.LastOrDefault(); var propName = ResolvePropertyName(Host, property); return(Host._SetNew(propName)); } }
public void SetValue(object value) { if (Bag != null) { Bag.Items.Add(value); return; } var bag = value as Bag; if (bag != null) { value = bag.Items; } if ((bag != null) && (Host is IDictionary map)) { var type = TypeOf.DictionaryValue(map); if (type == typeof(object)) { var attrs = Host._GetAttributes <KnownTypeAttribute>(); type = ( from attr in attrs where typeof(ICollection).IsAssignableFrom(attr.Type) && !typeof(IDictionary).IsAssignableFrom(attr.Type) select attr.Type ).FirstOrDefault(); if (type == null) { throw new NotSupportedException("Tipo de mapeamento não suportado: " + Host.GetType().FullName); } } value = Change.To(value, type); } var realName = properties.Last(); if (adders != null) { object key, val; foreach (var adder in adders) { var keyType = adder.GetParameters().First().ParameterType; var valType = adder.GetParameters().Last().ParameterType; if (Change.Try(realName, keyType, out key) && Change.Try(value, valType, out val)) { Host._Call("Add", key, val); return; } } } var propName = ResolvePropertyName(Host, realName); var factory = Host as IGraphFactory; if (bag != null) { if (bag.Items.Count == 0) { return; } if (factory != null) { for (var i = 0; i < bag.Items.Count; i++) { if (bag.Items[i] is HashMap item) { bag.Items[i] = factory.CreateItem(propName, item, new Mapper()) ?? item; } } value = factory.CreateList(propName, bag.Items) ?? bag; } } if (value is HashMap graph && factory != null) { value = factory.CreateItem(propName, graph, new Mapper()) ?? value; } Host._Set(propName, value); }