Example #1
0
        private static object CreateValueFromConfiguration(Type type, string value, IConfiguration configuration)
        {
            var typeInfo = type.GetTypeInfo();

            if (typeInfo.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                return(CreateValueFromConfiguration(Nullable.GetUnderlyingType(type), value, configuration));
            }

            var configurationValue = configuration.Get(key: null);

            try
            {
                if (typeInfo.IsEnum)
                {
                    return(Enum.Parse(type, configurationValue));
                }
                else
                {
                    return(Convert.ChangeType(configurationValue, type));
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(Resources.FormatError_FailedBinding(configurationValue, type), ex);
            }
        }
Example #2
0
        private static object ReadValue(Type type, string value, IConfigurationSection config)
        {
            if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                return(ReadValue(Nullable.GetUnderlyingType(type), value, config));
            }

            try
            {
                return(TypeDescriptor.GetConverter(type).ConvertFromInvariantString(config.Value));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(Resources.FormatError_FailedBinding(config.Value, type), ex);
            }
        }