예제 #1
0
        /// <summary>
        ///   Converts the constructor parameters.
        /// </summary>
        /// <param name = "constructor">The constructor.</param>
        /// <param name = "configuration">The configuration.</param>
        /// <returns></returns>
        private object[] ConvertConstructorParameters(ConstructorInfo constructor, IConfiguration configuration)
        {
            var conversionManager = ConversionManager;

            var parameters      = constructor.GetParameters();
            var parameterValues = new object[parameters.Length];

            for (int i = 0, n = parameters.Length; i < n; ++i)
            {
                var parameter = parameters[i];

                var paramConfig = FindChildIgnoreCase(configuration, parameter.Name);
                if (paramConfig == null)
                {
                    throw new ConverterException(string.Format("Child '{0}' missing in {1} element.", parameter.Name,
                                                               configuration.Name));
                }

                var paramType = parameter.ParameterType;
                if (!ConversionManager.CanHandleType(paramType))
                {
                    throw new ConverterException(string.Format("No converter found for child '{0}' in {1} element (type: {2}).",
                                                               parameter.Name, configuration.Name, paramType.Name));
                }

                parameterValues[i] = ConvertChildParameter(paramConfig, paramType);
            }

            return(parameterValues);
        }
예제 #2
0
        /// <summary>
        ///   Converts the property values.
        /// </summary>
        /// <param name = "instance">The instance.</param>
        /// <param name = "type">The type.</param>
        /// <param name = "configuration">The configuration.</param>
        private void ConvertPropertyValues(object instance, Type type, IConfiguration configuration)
        {
            var conversionManager = ConversionManager;

            foreach (var propConfig in configuration.Children)
            {
                var property =
                    type.GetProperty(propConfig.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                if (property == null || !property.CanWrite)
                {
                    continue;
                }

                var propType = property.PropertyType;
                if (!ConversionManager.CanHandleType(propType))
                {
                    throw new ConverterException(string.Format("No converter found for child '{0}' in {1} element (type: {2}).",
                                                               property.Name, configuration.Name, propType.Name));
                }

                var val = ConvertChildParameter(propConfig, propType);
                property.SetValue(instance, val, null);
            }
        }