Exemplo n.º 1
0
        public void Travel(IReadVisitor visitor, TDictionary graph)
        {
            var valueArgs = VisitArgs.DictionaryValue;

            TValue TravelValue()
            {
                TValue value;

                if (_valueVisitor != null)
                {
                    if (!_valueVisitor.TryVisitValue(visitor, valueArgs, out value))
                    {
                        throw new InvalidGraphException("There were no corresponding value to the dictionary key.");
                    }
                    return(value);
                }

                if (visitor.TryVisit(valueArgs) != ValueState.Found)
                {
                    throw new InvalidGraphException("There were no corresponding value to the dictionary key.");
                }
                value = (TValue)_instanceFactory.CreateInstance(_valueType);
                _valueTraveller.Travel(visitor, value);
                visitor.Leave(valueArgs);
                return(value);
            }

            var keyArgs = VisitArgs.DictionaryKey;

            if (_keyVisitor != null)
            {
                while (_keyVisitor.TryVisitValue(visitor, keyArgs, out var key))
                {
                    var value = TravelValue();
                    graph.Add(key, value);
                }
                return;
            }
            while (visitor.TryVisit(keyArgs) == ValueState.Found)
            {
                var key = (TKey)_instanceFactory.CreateInstance(_keyType);
                _keyTraveller.Travel(visitor, key);
                visitor.Leave(keyArgs);

                var value = TravelValue();
                graph.Add(key, value);
            }
        }
Exemplo n.º 2
0
        public void Travel(IReadVisitor visitor, TCollection graph)
        {
            var itemArgs = VisitArgs.CollectionItem;

            if (_valueVisitor != null)
            {
                while (_valueVisitor.TryVisitValue(visitor, itemArgs, out var value))
                {
                    graph.Add(value);
                }
                return;
            }
            while (visitor.TryVisit(itemArgs) == ValueState.Found)
            {
                var element = (TElement)_instanceFactory.CreateInstance(_elementType);
                _elementTraveller.Travel(visitor, element);
                graph.Add(element);
                visitor.Leave(itemArgs);
            }
        }