public static IParser CreateParser(Type targetType, ConfigurationBinderOptions options)
 {
     if (targetType == typeof(Guid))
     {
         return(new GuidParser());
     }
     else if (targetType == typeof(Uri))
     {
         return(new UriParser());
     }
     else if (targetType.IsEnumerable() && targetType != typeof(string))
     {
         return(new ArrayParser(options.ArraySeparator, targetType));
     }
     else if (targetType.BaseType == typeof(Enum))
     {
         return(new EnumParser(targetType));
     }
     else if (targetType == typeof(DateTime))
     {
         return(new DateTimeParser());
     }
     else
     {
         return(new DefaultParser(targetType));
     }
 }
        public static void BindSettings(
            this IConfiguration configuration,
            object target,
            ConfigurationBinderOptions options)
        {
            var type = target.GetType();

            var settableProperties = type
                                     .GetProperties()
                                     .Where(prop => prop.GetSetMethod() != null);

            var configKeyValuePairs = configuration
                                      .AsEnumerable()
                                      .ToList();

            foreach (var prop in settableProperties)
            {
                var key = $"{type.Name}{options.KeySeparator}{prop.Name}";

                string value = configKeyValuePairs
                               .FirstOrDefault(kv => kv.Key.Equals(key, options.KeyComparison))
                               .Value;

                if (value != null)
                {
                    object propertyValue = ParserFactory
                                           .CreateParser(prop.PropertyType, options)
                                           .Parse(value);

                    prop.Assign(target, propertyValue);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Finds keys in the format "ObjectTypeName.PropertyName"
        /// and binds them to the corresponding properties on the target type
        /// </summary>
        /// <param name="services">Service Collection</param>
        /// <param name="options">Configuration Binder options</param>
        /// <typeparam name="TConfig">Target type to bind settings to</typeparam>
        public static IServiceCollection AddConfiguration <TConfig>(
            this IServiceCollection services,
            ConfigurationBinderOptions options)
            where TConfig : class, new()
        {
            services.AddOptions <TConfig>()
            .Configure <IConfiguration>((target, configuration) =>
                                        configuration.BindSettings(target, options));

            return(services);
        }
Example #4
0
        public void BindWithCustomKeySeparator()
        {
            var target        = new ConfigurationObject();
            var customOptions = new ConfigurationBinderOptions {
                KeySeparator = "_"
            };

            var data = new Dictionary <string, string>
            {
                { "configurationObject_string", "abc" }
            };

            var expectedValue = "abc";

            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(data)
                                .Build();

            Assert.DoesNotThrow(() => configuration.BindSettings(target, customOptions));

            Assert.AreEqual(expectedValue, target.String);
        }