示例#1
0
        /// <summary>
        /// Cria um valor compatível para o tipo indicado com base no conteúdo
        /// do modelo deserializado.
        /// </summary>
        /// <param name="type">O tipo do dado esperado.</param>
        /// <param name="node">O modelo deserializado.</param>
        /// <returns>O valor obtido da compatibilização.</returns>
        private object CreateCompatibleValue(Type type, NodeModel node)
        {
            if (node == null)
            {
                return(null);
            }

            if (type == typeof(PropertyCollection))
            {
                return((PropertyCollection)CreateCompatibleValue(typeof(object), node));
            }

            if (type == typeof(NameCollection))
            {
                var target = new NameCollection();
                foreach (var item in node.ChildValues())
                {
                    target.Add(item.Value.ToString());
                }
                return(target);
            }

            if (type == typeof(FieldCollection))
            {
                var target = new FieldCollection();
                foreach (var @object in node.ChildObjects())
                {
                    var field = new Field();
                    CopyNodeProperties(@object, field);
                    target.Add(field);
                }
                return(target);
            }

            if (type == typeof(LinkCollection))
            {
                var target = new LinkCollection();
                foreach (var item in node.Children())
                {
                    var link = new Link();
                    CopyNodeProperties(item, link);
                    target.Add(link);
                }
                return(target);
            }

            if (type == typeof(EntityActionCollection))
            {
                var target = new EntityActionCollection();
                foreach (var item in node.Children())
                {
                    var action = new EntityAction();
                    CopyNodeProperties(item, action);
                    target.Add(action);
                }
                return(target);
            }

            if (type == typeof(EntityCollection))
            {
                var target = new EntityCollection();
                foreach (var item in node.Children())
                {
                    var entity = new Entity();
                    CopyNodeProperties(item, entity);
                    target.Add(entity);
                }
                return(target);
            }

            if (type == typeof(CaseVariantString))
            {
                var text = (node as ValueModel)?.Value.ToString();
                return(text.ChangeCase(TextCase.PascalCase));
            }

            return(CreateCompatibleValue(node));
        }