Пример #1
0
        public static IConfiguration AssignValues <T>([NotNull] this IConfiguration configuration, [NotNull] T obj, [CanBeNull] string instance = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var settingProperties =
                typeof(T)
                .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .Where(p => p.GetCustomAttribute <SettingMemberAttribute>() != null);

            foreach (var property in settingProperties)
            {
                // Create a lambda-expression so that we can reuse the extensions for it we already have.
                var expression = Expression.Lambda(
                    Expression.Property(
                        Expression.Constant(obj),
                        property.Name
                        )
                    );

                var settingInfo = SettingMetadata.FromExpression(expression, false);
                //var value = configuration.GetValue(settingInfo.SettingName, property.PropertyType, settingInfo.ProviderName);
                //settingInfo.SetValue(value);
            }

            return(configuration);
        }
Пример #2
0
        public static IConfiguration SetValue <T>([NotNull] this IConfiguration config, [NotNull] Expression <Func <T> > expression, [CanBeNull] T value, [CanBeNull] string instance = null)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var settingInfo = SettingMetadata.FromExpression(expression, false);
            var query       = ValueQueryFactory.CreateSetValueQuery(settingInfo, value, instance);

            //var settingValue = config.GetValue(settingInfo.SettingName, typeof(T), settingInfo.ProviderName) ?? settingInfo.DefaultValue;

            //var settingValue = //settingInfo.GetValue();

            settingInfo
            .Validations
            .Validate(query.SettingName, value);

            config.SetValue(query);
            return(config);
        }
Пример #3
0
        public static IConfiguration AssignValue <T>([NotNull] this IConfiguration configuration, [NotNull] Expression <Func <T> > expression, [CanBeNull] string instance = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var settingContext = SettingMetadata.FromExpression(expression, false);

            var value = configuration.GetValue(expression, instance);

            settingContext.SetValue(value);

            return(configuration);
        }
Пример #4
0
        private static T GetValue <T>([NotNull] this IConfiguration config, [NotNull] LambdaExpression expression, [CanBeNull] string instanceName = null)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var settingMetadata = SettingMetadata.FromExpression(expression, false);
            var query           = ValueQueryFactory.CreateGetValueQuery(settingMetadata, instanceName);

            var settingValue = config.GetValue(query) ?? settingMetadata.DefaultValue;

            settingMetadata
            .Validations
            .Validate(query.SettingName, settingValue);

            return((T)settingValue);
        }