예제 #1
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            var formatProvider = (IFormatProvider)propertyMapData.TypeConverterOptions.CultureInfo;

            TimeSpan span;

            #if !NET_2_0 && !NET_3_5 && !PCL
            var timeSpanStyle = propertyMapData.TypeConverterOptions.TimeSpanStyle ?? TimeSpanStyles.None;
            if( !string.IsNullOrEmpty( propertyMapData.TypeConverterOptions.Format ) && TimeSpan.TryParseExact( text, propertyMapData.TypeConverterOptions.Format, formatProvider, timeSpanStyle, out span ) )
            {
                return span;
            }

            if( string.IsNullOrEmpty( propertyMapData.TypeConverterOptions.Format ) && TimeSpan.TryParse( text, formatProvider, out span ) )
            {
                return span;
            }
            #else
            if( TimeSpan.TryParse( text, out span ) )
            {
                return span;
            }
            #endif

            return base.ConvertFromString( text, row, propertyMapData );
        }
예제 #2
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            if( text == null )
            {
                return base.ConvertFromString( text, row, propertyMapData );
            }

            return new Guid( text );
        }
예제 #3
0
 /// <summary>
 /// Converts the string to an object.
 /// </summary>
 /// <param name="text">The string to convert to an object.</param>
 /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
 /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
 /// <returns>The object created from the string.</returns>
 public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
 {
     try
     {
         return Enum.Parse( type, text, true );
     }
     catch
     {
         return base.ConvertFromString( text, row, propertyMapData );
     }
 }
예제 #4
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            var numberStyle = propertyMapData.TypeConverterOptions.NumberStyle ?? NumberStyles.Float;

            decimal d;
            if( decimal.TryParse( text, numberStyle, propertyMapData.TypeConverterOptions.CultureInfo, out d ) )
            {
                return d;
            }

            return base.ConvertFromString( text, row, propertyMapData );
        }
예제 #5
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            var numberStyle = propertyMapData.TypeConverterOptions.NumberStyle ?? NumberStyles.Integer;

            sbyte sb;
            if( sbyte.TryParse( text, numberStyle, propertyMapData.TypeConverterOptions.CultureInfo, out sb ) )
            {
                return sb;
            }

            return base.ConvertFromString( text, row, propertyMapData );
        }
예제 #6
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            if( text == null )
            {
                return base.ConvertFromString( null, row, propertyMapData );
            }

            var formatProvider = (IFormatProvider)propertyMapData.TypeConverterOptions.CultureInfo.GetFormat( typeof( DateTimeFormatInfo ) ) ?? propertyMapData.TypeConverterOptions.CultureInfo;
            var dateTimeStyle = propertyMapData.TypeConverterOptions.DateTimeStyle ?? DateTimeStyles.None;

            return string.IsNullOrEmpty( propertyMapData.TypeConverterOptions.Format )
                ? DateTime.Parse( text, formatProvider, dateTimeStyle )
                : DateTime.ParseExact( text, propertyMapData.TypeConverterOptions.Format, formatProvider, dateTimeStyle );
        }
예제 #7
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            if( text != null && text.Length > 1 )
            {
                text = text.Trim();
            }

            char c;
            if( char.TryParse( text, out c ) )
            {
                return c;
            }

            return base.ConvertFromString( text, row, propertyMapData );
        }
예제 #8
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            Array array;
            var type = propertyMapData.Member.MemberType().GetElementType();

            if( propertyMapData.IsNameSet || row.Configuration.HasHeaderRecord && !propertyMapData.IsIndexSet )
            {
                // Use the name.
                var list = new List<object>();
                var nameIndex = 0;
                while( true )
                {
                    object field;
                    if( !row.TryGetField( type, propertyMapData.Names.FirstOrDefault(), nameIndex, out field ) )
                    {
                        break;
                    }

                    list.Add( field );
                    nameIndex++;
                }

                array = (Array)ReflectionHelper.CreateInstance( propertyMapData.Member.MemberType(), list.Count );
                for( var i = 0; i < list.Count; i++ )
                {
                    array.SetValue( list[i], i );
                }
            }
            else
            {
                // Use the index.
                var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                    ? row.CurrentRecord.Length - 1
                    : propertyMapData.IndexEnd;

                var arraySize = indexEnd - propertyMapData.Index + 1;
                array = (Array)ReflectionHelper.CreateInstance( propertyMapData.Member.MemberType(), arraySize );
                var arrayIndex = 0;
                for( var i = propertyMapData.Index; i <= indexEnd; i++ )
                {
                    array.SetValue( row.GetField( type, i ), arrayIndex );
                    arrayIndex++;
                }
            }

            return array;
        }
예제 #9
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            if( text == null )
            {
                return string.Empty;
            }

            foreach( var nullValue in propertyMapData.TypeConverterOptions.NullValues )
            {
                if( text == nullValue )
                {
                    return null;
                }
            }

            return text;
        }
예제 #10
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            var dictionary = new Dictionary<string, string>();

            var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                ? row.CurrentRecord.Length - 1
                : propertyMapData.IndexEnd;

            for( var i = propertyMapData.Index; i <= indexEnd; i++ )
            {
                string field;
                if( row.TryGetField( i, out field ) )
                {
                    dictionary.Add( row.FieldHeaders[i], field );
                }
            }

            return dictionary;
        }
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            var keyType = propertyMapData.Member.MemberType().GetGenericArguments()[0];
            var valueType = propertyMapData.Member.MemberType().GetGenericArguments()[1];
            var dictionaryType = typeof( Dictionary<,> );
            dictionaryType = dictionaryType.MakeGenericType( keyType, valueType );
            var dictionary = (IDictionary)ReflectionHelper.CreateInstance( dictionaryType );

            var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                ? row.CurrentRecord.Length - 1
                : propertyMapData.IndexEnd;

            for( var i = propertyMapData.Index; i <= indexEnd; i++ )
            {
                var field = row.GetField( valueType, i );

                dictionary.Add( row.FieldHeaders[i], field );
            }

            return dictionary;
        }
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            // Since we're using the PropertyType here, this converter can be used for multiple types
            // as long as they implement IList.
            var list = (IList)ReflectionHelper.CreateInstance( propertyMapData.Member.MemberType() );
            var type = propertyMapData.Member.MemberType().GetGenericArguments()[0];

            if( propertyMapData.IsNameSet || row.Configuration.HasHeaderRecord && !propertyMapData.IsIndexSet )
            {
                // Use the name.
                var nameIndex = 0;
                while( true )
                {
                    object field;
                    if( !row.TryGetField( type, propertyMapData.Names.FirstOrDefault(), nameIndex, out field ) )
                    {
                        break;
                    }

                    list.Add( field );
                    nameIndex++;
                }
            }
            else
            {
                // Use the index.
                var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                    ? row.CurrentRecord.Length - 1
                    : propertyMapData.IndexEnd;

                for( var i = propertyMapData.Index; i <= indexEnd; i++ )
                {
                    var field = row.GetField( type, i );

                    list.Add( field );
                }
            }

            return list;
        }
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            var type = propertyMapData.Member.MemberType().GetGenericArguments()[0];
            var listType = typeof( List<> );
            listType = listType.MakeGenericType( type );
            var list = (IList)ReflectionHelper.CreateInstance( listType );

            if( propertyMapData.IsNameSet || row.Configuration.HasHeaderRecord && !propertyMapData.IsIndexSet )
            {
                // Use the name.
                var nameIndex = 0;
                while( true )
                {
                    object field;
                    if( !row.TryGetField( type, propertyMapData.Names.FirstOrDefault(), nameIndex, out field ) )
                    {
                        break;
                    }

                    list.Add( field );
                    nameIndex++;
                }
            }
            else
            {
                // Use the index.
                var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                    ? row.CurrentRecord.Length - 1
                    : propertyMapData.IndexEnd;

                for( var i = propertyMapData.Index; i <= indexEnd; i++ )
                {
                    var field = row.GetField( type, i );

                    list.Add( field );
                }
            }

            return list;
        }
예제 #14
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            bool b;
            if( bool.TryParse( text, out b ) )
            {
                return b;
            }

            short sh;
            if( short.TryParse( text, out sh ) )
            {
                if( sh == 0 )
                {
                    return false;
                }
                if( sh == 1 )
                {
                    return true;
                }
            }

            var t = ( text ?? string.Empty ).Trim();
            foreach( var trueValue in propertyMapData.TypeConverterOptions.BooleanTrueValues )
            {
                if( propertyMapData.TypeConverterOptions.CultureInfo.CompareInfo.Compare( trueValue, t, CompareOptions.IgnoreCase ) == 0 )
                {
                    return true;
                }
            }

            foreach( var falseValue in propertyMapData.TypeConverterOptions.BooleanFalseValues )
            {
                if( propertyMapData.TypeConverterOptions.CultureInfo.CompareInfo.Compare( falseValue, t, CompareOptions.IgnoreCase ) == 0 )
                {
                    return false;
                }
            }

            return base.ConvertFromString( text, row, propertyMapData );
        }
예제 #15
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            var list = new ArrayList();

            if( propertyMapData.IsNameSet || row.Configuration.HasHeaderRecord && !propertyMapData.IsIndexSet )
            {
                // Use the name.
                var nameIndex = 0;
                while( true )
                {
                    string field;
                    if( !row.TryGetField( propertyMapData.Names.FirstOrDefault(), nameIndex, out field ) )
                    {
                        break;
                    }

                    list.Add( field );
                    nameIndex++;
                }
            }
            else
            {
                // Use the index.
                var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                    ? row.CurrentRecord.Length - 1
                    : propertyMapData.IndexEnd;

                for( var i = propertyMapData.Index; i <= indexEnd; i++ )
                {
                    string field;
                    if( row.TryGetField( i, out field ) )
                    {
                        list.Add( field );
                    }
                }
            }

            return list;
        }
        public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
        {
            var trimmed = text.Trim(new char[] { ' ', '"', '\n' });

            return(TimeUtils.DateStringToDateTimeUtc(trimmed));
        }
예제 #17
0
 /// <summary>
 /// Converts the string to an object.
 /// </summary>
 /// <param name="text">The string to convert to an object.</param>
 /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
 /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
 /// <returns>The object created from the string.</returns>
 public virtual object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
 {
     throw new CsvTypeConverterException( "The conversion cannot be performed." );
 }
예제 #18
0
        /// <summary>
        /// Get bounce category
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        private BounceCat GetBounceCategory(ICsvReaderRow b)
        {
            var       value = b.GetField <string>("bounceCat");
            BounceCat bc    = BounceCat.Other;

            switch (value)
            {
            case "other": bc = BounceCat.Other;
                break;

            case "bad-mailbox": bc = BounceCat.BadMailbox;
                break;

            case "bad-domain": bc = BounceCat.BadDomain;
                break;

            case "inactive-mailbox": bc = BounceCat.InactiveMailbox;
                break;

            case "bad-configuration": bc = BounceCat.BadConfiguration;
                break;

            case "bad-connection": bc = BounceCat.BadConnection;
                break;

            case "content-related": bc = BounceCat.ContentRelated;
                break;

            case "invalid-sender": bc = BounceCat.InvalidSender;
                break;

            case "message-expired": bc = BounceCat.MessageExpired;
                break;

            case "no-answer-from-host": bc = BounceCat.NoAnswerFromHost;
                break;

            case "policy-related": bc = BounceCat.PolicyRelated;
                break;

            case "protocol-errors": bc = BounceCat.ProtocolErrors;
                break;

            case "quota-issues": bc = BounceCat.QuotaIssues;
                break;

            case "relaying-issues": bc = BounceCat.RelayingIssues;
                break;

            case "routing-errors": bc = BounceCat.RoutingErrors;
                break;

            case "spam-related": bc = BounceCat.SpamRelated;
                break;

            case "virus-related": bc = BounceCat.VirusRelated;
                break;

            default:
                break;
            }
            return(bc);
        }
예제 #19
0
 public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
 {
     return("test");
 }
예제 #20
0
 /// <summary>
 /// Throws an exception.
 /// </summary>
 /// <param name="text">The string to convert to an object.</param>
 /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
 /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
 /// <returns>The object created from the string.</returns>
 public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
 {
     throw new CsvTypeConverterException("Converting IEnumerable types is not supported for a single field. " +
                                         "If you want to do this, create your own ITypeConverter and register " +
                                         "it in the TypeConverterFactory by calling AddConverter.");
 }
예제 #21
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
        {
            if( string.IsNullOrEmpty( text ) )
            {
                return null;
            }

            foreach( var nullValue in propertyMapData.TypeConverterOptions.NullValues )
            {
                if( text == nullValue )
                {
                    return null;
                }
            }

            return UnderlyingTypeConverter.ConvertFromString( text, row, propertyMapData );
        }
예제 #22
0
 /// <summary>
 /// Converts the string to an object.
 /// </summary>
 /// <param name="text">The string to convert to an object.</param>
 /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
 /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
 /// <returns>The object created from the string.</returns>
 public virtual object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
 {
     throw new CsvTypeConverterException("The conversion cannot be performed.");
 }
예제 #23
0
 /// <summary>
 /// Throws an exception.
 /// </summary>
 /// <param name="text">The string to convert to an object.</param>
 /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
 /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
 /// <returns>The object created from the string.</returns>
 public override object ConvertFromString( string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData )
 {
     throw new CsvTypeConverterException( "Converting IEnumerable types is not supported for a single field. " +
                                          "If you want to do this, create your own ITypeConverter and register " +
                                          "it in the TypeConverterFactory by calling AddConverter." );
 }
예제 #24
0
 public object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
 {
     throw new NotImplementedException();
 }
        public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
        {
            var dequotedText = text.Trim(new char[] { ' ', '"' });

            return(base.ConvertFromString(dequotedText, row, propertyMapData));
        }
예제 #26
0
 private static string GetColumn(ICsvReaderRow csv, string columnName)
 {
     return(csv.GetField <string>(columnName).Trim());
 }