Ejemplo n.º 1
0
        /// <summary>
        /// Uses the supplied <see cref="IParserContext"/> to write all values to properties.
        /// </summary>
        /// <param name="parserContext">The supplied <see cref="IParserContext"/>.</param>
        /// <exception cref="ArgumentNullException">The value of '<paramref name="parserContext"/>' cannot be null. </exception>
        /// <exception cref="PropertyMustBeSpecifiedException">The 'ValueProcessor' property of one of the parameters inside the <see cref="IParserContext"/> is null.</exception>
        public void Parse(IParserContext parserContext)
        {
            if (parserContext == null)
            {
                throw new ArgumentNullException(nameof(parserContext));
            }

            foreach (var propertyMapping in parserContext.Maps.Join(parserContext.QueryValuePairs, map => map.Parameter.ParameterName, pair => pair.Key, (map, pair) =>
            {
                map.Parameter.Value = pair.Value.Value;
                return(map);
            }))
            {
                if (propertyMapping.Parameter.ValueProcessor == null)
                {
                    throw new PropertyMustBeSpecifiedException("ValueProcessor", propertyMapping.Parameter);
                }

                if (propertyMapping.Parameter.Value == null)
                {
                    throw new PropertyMustBeSpecifiedException("Value", propertyMapping.Parameter);
                }

                if (propertyMapping.Parameter.TypeOfValue == null)
                {
                    throw new PropertyMustBeSpecifiedException("TypeOfValue", propertyMapping.Parameter);
                }

                var childProcessingContext = parserContext.CreateProcessingContext(propertyMapping.Parameter.Value.ToString(), propertyMapping.Parameter.TypeOfValue);
                var resolvedValue          = propertyMapping.Parameter.ValueProcessor.Resolve(childProcessingContext);

                propertyMapping.Property.SetValue(propertyMapping.Source, resolvedValue);
            }
        }