Beispiel #1
0
        /// <summary>
        /// Convert a field value into a write able value
        /// </summary>
        /// <param name="fieldValue">object value to convert</param>
        /// <returns>converted value</returns>
        public object CreateValueForField(object fieldValue)
        {
            object val = null;

            if (fieldValue == null)
            {
                if (NullValue == null)
                {
                    //Did not respect nullable type, so hence second part of boolean expression
                    if (FieldTypeInternal.IsValueType && !IsNullableType)
                    {
                        throw new BadUsageException("Null Value found. You must specify a NullValueAttribute in the " + FieldInfo.Name +
                                                    " field of type " + FieldTypeInternal.Name + ", because this is a ValueType.");
                    }
                    else
                    {
                        val = null;
                    }
                }
                else
                {
                    val = NullValue;
                }
            }
            else if (FieldTypeInternal == fieldValue.GetType())
            {
                val = fieldValue;
            }
            else
            {
                if (ConvertProvider == null)
                {
                    val = Convert.ChangeType(fieldValue, FieldTypeInternal, null);
                }
                else
                {
                    try
                    {
                        val = Convert.ChangeType(fieldValue, FieldTypeInternal, null);
                    }
                    catch
                    {
                        val = ConvertProvider.StringToField(fieldValue.ToString());
                    }
                }
            }

            return(val);
        }
Beispiel #2
0
        /// <summary>
        /// Create field object after extracting the string from the underlying
        /// input data
        /// </summary>
        /// <param name="fieldString">Information extracted?</param>
        /// <param name="line">Underlying input data</param>
        /// <returns>Object to assign to field</returns>
        private AssignResult AssignFromString(ExtractedInfo fieldString, LineInfo line)
        {
            object val;

            var extractedString = fieldString.ExtractedString();

            try
            {
                if (ConvertProvider == null)
                {
                    if (IsStringField)
                    {
                        val = TrimString(extractedString);
                    }
                    else
                    {
                        extractedString = extractedString.Trim();

                        if (extractedString.Length == 0)
                        {
                            return(new AssignResult {
                                Value = GetNullValue(line), NullValueUsed = true
                            });
                        }
                        else
                        {
                            val = Convert.ChangeType(extractedString, FieldTypeInternal, null);
                        }
                    }
                }
                else
                {
                    var trimmedString = extractedString.Trim();

                    if (ConvertProvider.CustomNullHandling == false &&
                        trimmedString.Length == 0)
                    {
                        return(new AssignResult {
                            Value = GetNullValue(line), NullValueUsed = true
                        });
                    }
                    else
                    {
                        if (TrimMode == FileHelpers.TrimMode.Both)
                        {
                            val = ConvertProvider.StringToField(trimmedString);
                        }
                        else
                        {
                            val = ConvertProvider.StringToField(TrimString(extractedString));
                        }

                        if (val == null)
                        {
                            return new AssignResult {
                                       Value = GetNullValue(line), NullValueUsed = true
                            }
                        }
                        ;
                    }
                }

                return(new AssignResult {
                    Value = val
                });
            }
            catch (ConvertException ex)
            {
                ex.FieldName    = FieldInfo.Name;
                ex.LineNumber   = line.mReader.LineNumber;
                ex.ColumnNumber = fieldString.ExtractedFrom + 1;
                throw;
            }
            catch (BadUsageException)
            {
                throw;
            }
            catch (Exception ex)
            {
                if (ConvertProvider == null || ConvertProvider.GetType().Assembly == typeof(FieldBase).Assembly)
                {
                    throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, ex.Message, ex);
                }
                else
                {
                    throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, "Your custom converter: " + ConvertProvider.GetType().Name + " throws an " + ex.GetType().Name + " with the message: " + ex.Message, ex);
                }
            }
        }