Exemple #1
0
        /// <summary>
        /// This method tries to process given line according to given set of parameters.
        /// </summary>
        /// <remarks>
        /// This method processes given line by splitting it into its pieces and then by
        /// trying to convert each element into its type-safe object expression.
        /// </remarks>
        /// <param name="line">
        /// The line to be processed.
        /// </param>
        /// <param name="separator">
        /// The separator to be used to split given line at.
        /// </param>
        /// <param name="exactly">
        /// An exception of type 'FormatException' is throw if this parameter is 'true' and
        /// converting one of the values has failed. Otherwise, each variable is set to a
        /// default value. Which default value will be used in such a failure case depends
        /// on current data type. For example, strings using &lt;null&gt; as default value,
        /// 'false' is used as default for Boolean types, and the corresponding max-value is
        /// used for all other types.
        /// </param>
        /// <param name="culture">
        /// The culture information to be used for data conversion.
        /// </param>
        /// <param name="mapping">
        /// The value mappings to be used for an extended data interpretation.
        /// </param>
        /// <param name="descriptors">
        /// A list of data type descriptor items that help interpreting data.
        /// </param>
        /// <returns>
        /// An instance of class of type <typeparamref name="TInstance"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// This exception is thrown for example if <typeparamref name="TInstance"/> does not
        /// provide a default constructor.
        /// </exception>
        /// <exception cref="FormatException">
        /// This exception is thrown for example if 'exactly' is set to 'true' and one of the
        /// data items could not be converted.
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// This exception is thrown as soon as a class of type <typeparamref name="TInstance"/>
        /// wants to use an unsupported data type.
        /// </exception>
        private static TInstance ProcessLine(String line, Char separator, Boolean exactly, CultureInfo culture, CsvMappings mapping, List <ItemDescriptor> descriptors)
        {
            TInstance result = CsvImporter <TInstance> .Construct(); // throws

            List <String> values = ProcessHelper.SplitIntoCells(line, separator);

            for (Int32 index = 0; index < values.Count; index++)
            {
                // A simple break is possible because of column validation has been done already.
                if (index >= descriptors.Count)
                {
                    break;
                }

                String       value    = values[index];
                PropertyInfo property = descriptors[index].Origin;
                Type         type     = property.PropertyType;

                property.SetValue(result, TypeConverter.IntoObject(value, type, exactly, culture, mapping)); // throws
            }

            return(result);
        }