예제 #1
0
        private void SetValuesFromProviders(string path, Type type, IDictionary <string, object> obj)
        {
            var propertyInfos = ReflectionUtils.GetProperties(type);

            foreach (var property in propertyInfos)
            {
                var propertyPath = path + "." + property.Name;

                if (ReflectionUtils.IsNestedParameterProperty(property))
                {
                    SetValuesFromProviders(propertyPath, property.PropertyType,
                                           obj[property.Name] as IDictionary <string, object>);
                    continue;
                }

                Value?value = null;
                foreach (var provider in _valueProviders)
                {
                    value = provider.GetValue(propertyPath);
                    if (value != null)
                    {
                        break;
                    }
                }

                if (value == null)
                {
                    continue;
                }

                obj[property.Name] = ValueDeserialiser.GetValue(value.Value.ValueToSet, property.PropertyType,
                                                                property.Name, "setting value from value provider");
                obj[property.Name + Consts.SourceMetadata] = value.Value.SourceHint;
            }
        }
예제 #2
0
        private static void FillObjectDefaults(string path, Type type, IDictionary <string, object> obj)
        {
            var propertyInfos = ReflectionUtils.GetProperties(type);

            foreach (var property in propertyInfos)
            {
                if (ReflectionUtils.IsNestedParameterProperty(property))
                {
                    var child = new ExpandoObject() as IDictionary <string, object>;
                    FillObjectDefaults($"{path}.{property.Name}", property.PropertyType, child);
                    obj.Add(property.Name, child);

                    var overrides = ReflectionUtils.GetAttributes <OverrideAttribute>(property);
                    foreach (var overrideAttribute in overrides)
                    {
                        var overrideDetails = new OverrideDetails(overrideAttribute, type, property);
                        ReflectionUtils.SetValue(child, overrideDetails, $"{path}.{property.Name}");
                    }
                    continue;
                }

                var value = ReflectionUtils.HasDefaultAttribute(property, out var defaultAttr)
                    ? ValueDeserialiser.GetValue(defaultAttr.Value, property.PropertyType, property.Name,
                                                 "setting default value")
                    : ReflectionUtils.MakeDefault(property.PropertyType);

                obj.Add(property.Name, value);
                obj.Add(property.Name + Consts.SourceMetadata, "Default");
            }
        }
예제 #3
0
        public static void SetValue(IDictionary <string, object> child, OverrideDetails details, string root)
        {
            var path           = details.Attribute.Path;
            var pathParts      = path.Split(Consts.PathSeperator);
            var obj            = child;
            var containingType = details.Property.PropertyType;

            foreach (var pathPart in pathParts.Take(pathParts.Count() - 1))
            {
                containingType = GetPropertyType(containingType, pathPart, details);
                obj            = obj[pathPart] as IDictionary <string, object>;
                if (obj == null)
                {
                    throw new ProgrammerErrorException("We managed to find the type via reflection, but our " +
                                                       "object holding the data is formed to match.");
                }
            }

            var targetPropertyName = pathParts.Last();
            var targetPropertyType = GetPropertyType(containingType, targetPropertyName, details);

            obj[targetPropertyName] = ValueDeserialiser.GetValue(details.Attribute.Value, targetPropertyType,
                                                                 $"{root}.{path}", $"setting override from property '{details.Property.Name}' in " +
                                                                 $"type '{details.ContainingType}'");

            var sourcePath = root.Substring(root.IndexOf('.') + 1); // Trim the top root

            obj[targetPropertyName + Consts.SourceMetadata] = $"Override on {sourcePath}";
        }