Esempio n. 1
0
        private T Map(IMarkupDocument <T> doc, MarkupNode node, T parent)
        {
            var obj  = this.CreateNode(node.Type);
            var type = obj.GetType();

            var addMethods = type
                             .GetMethods()
                             .Where(m => (m.Name == "Add"))
                             .Where(m => (m.GetParameters().Length == 1))
                             .ToDictionary(m => m.GetParameters()[0].ParameterType);

            foreach (var attrib in node.Attributes)
            {
                this.SetProperty(obj, attrib.Value);
            }

            foreach (var child in node.Children)
            {
                var childInstance = Map(doc, child, obj);

                bool success = this.AddChildNode(obj, childInstance);

                if ((child.ID != null) && (obj is INamedNodeContainer))
                {
                    ((INamedNodeContainer)obj).SetChildNodeName(childInstance, child.ID);
                    success = true;
                }

                if (!success)
                {
                    throw new InvalidOperationException($"Could not find any Add method that can add a {childInstance.GetType().Name}.");
                }
            }

            if ((node.ID != null) && (!(parent is INamedNodeContainer) || !this.swallowsNamedNodeInDocument))
            {
                doc.SetNodeName(obj, node.ID);
            }

            doc.NotifyCreateNode(obj);

            return(obj);
        }
Esempio n. 2
0
 public T Map(IMarkupDocument <T> doc, MarkupNode node) => this.Map(doc, node, null);