Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
        }