Ejemplo n.º 1
0
        private static object BindToCollection(Type type, IConfiguration config, FastBinderOptions options)
        {
            var    box      = ListGenerator.Instance.EnsureCreate(type.GenericTypeArguments[0]);
            object instance = box.Newer();

            BindCollection(instance, box.GenType, config, options);
            return(instance);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to bind the configuration instance to a new instance of type T.
        /// If this configuration section has a value, that will be used.
        /// Otherwise binding by matching property names against configuration keys recursively.
        /// </summary>
        /// <param name="configuration">The configuration instance to bind.</param>
        /// <param name="type">The type of the new instance to bind.</param>
        /// <param name="configureOptions">Configures the binder options.</param>
        /// <returns>The new instance if successful, null otherwise.</returns>
        public static object FastGet(this IConfiguration configuration,
                                     Type type,
                                     Action <FastBinderOptions> configureOptions)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var options = new FastBinderOptions();

            configureOptions?.Invoke(options);
            return(BindInstance(type, instance: null, config: configuration, options: options));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.
        /// </summary>
        /// <param name="configuration">The configuration instance to bind.</param>
        /// <param name="instance">The object to bind.</param>
        /// <param name="configureOptions">Configures the binder options.</param>
        public static void FastBind(this IConfiguration configuration, object instance, Action <FastBinderOptions> configureOptions)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (instance != null)
            {
                var options = new FastBinderOptions();
                configureOptions?.Invoke(options);
                BindInstance(instance.GetType(), instance, configuration, options);
            }
        }
Ejemplo n.º 4
0
        private static void BindProperty(PropertyInfo property, object instance, IConfiguration config, FastBinderOptions options)
        {
            // We don't support set only, non public, or indexer properties
            if (property.GetMethod == null ||
                (!options.BindNonPublicProperties && !property.GetMethod.IsPublic) ||
                property.GetMethod.GetParameters().Length > 0)
            {
                return;
            }

            bool hasSetter = property.SetMethod != null && (property.SetMethod.IsPublic || options.BindNonPublicProperties);

            if (!hasSetter)
            {
                // The property cannot be set so there is no point going further
                return;
            }

            object propertyValue = GetPropertyValue(property, instance, config, options);

            if (propertyValue != null)
            {
                AutoSetValue(property, instance, propertyValue);
            }
        }
Ejemplo n.º 5
0
        private static void BindNonScalar(this IConfiguration configuration, object instance, FastBinderOptions options)
        {
            if (instance != null)
            {
                List <PropertyInfo> modelProperties = GetAllProperties(instance.GetType());

                if (options.ErrorOnUnknownConfiguration)
                {
                    HashSet <string> propertyNames = new HashSet <string>(modelProperties.Select(mp => mp.Name),
                                                                          StringComparer.OrdinalIgnoreCase);

                    IEnumerable <IConfigurationSection> configurationSections = configuration.GetChildren();
                    var missingPropertyNames = configurationSections
                                               .Where(cs => !propertyNames.Contains(cs.Key))
                                               .Select(mp => $"'{mp.Key}'");

                    if (missingPropertyNames.Any())
                    {
                        throw new InvalidOperationException(string.Format(Strings.Error_MissingConfig,
                                                                          nameof(options.ErrorOnUnknownConfiguration), nameof(FastBinderOptions), instance.GetType(),
                                                                          string.Join(", ", missingPropertyNames)));
                    }
                }

                foreach (PropertyInfo property in modelProperties)
                {
                    BindProperty(property, instance, configuration, options);
                }
            }
        }