示例#1
0
        private Maybe <RestValue> GetPropertyValue(RestValue sourceValue, BindingPath bindingPath, int pathIndex)
        {
            if (sourceValue.IsNull)
            {
                return(Maybe.Nothing <RestValue>());
            }

            var propertyName = bindingPath[pathIndex];
            var propertyInfo = sourceValue.Type.GetProperty(propertyName);

            if (propertyInfo == null)
            {
                throw new ArgumentException($"Invalid property name '{propertyName}'", nameof(bindingPath));
            }

            var value      = propertyInfo.GetValue(sourceValue.Value, null);
            var modelValue = new RestValue(value, propertyInfo.PropertyType);

            if (pathIndex + 1 == bindingPath.Length)
            {
                return(modelValue);
            }

            return(GetPropertyValue(modelValue, bindingPath, pathIndex + 1));
        }
示例#2
0
        public bool TryGetValue(string sourceName, BindingPath propertyPath, out RestValue value)
        {
            sourceName.AssertNotNull(nameof(sourceName));

            if (_values != null && _values.TryGetValue(sourceName, out var entry))
            {
                if (propertyPath.IsEmpty)
                {
                    value = entry;
                    return(true);
                }

                var propertyValue = GetPropertyValue(entry, propertyPath, 0);
                if (propertyValue.HasValue)
                {
                    value = propertyValue.Value;
                    return(true);
                }
            }

            value = null;
            return(false);
        }