public void ComponentModelCompatibilityTest()
        {
            var converter = new DateTimeOffsetConverter();
            var cmConverter = new System.ComponentModel.DateTimeOffsetConverter();

            var propertyMapData = new CsvPropertyMapData( null );
            propertyMapData.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            try
            {
                cmConverter.ConvertFromString( null );
                Assert.Fail();
            }
            catch( NotSupportedException ) { }

            try
            {
                converter.ConvertFromString( null, null, propertyMapData );
                Assert.Fail();
            }
            catch( CsvTypeConverterException ) { }

            try
            {
                cmConverter.ConvertFromString( "blah" );
                Assert.Fail();
            }
            catch( FormatException ) { }

            try
            {
                converter.ConvertFromString( "blah", null, propertyMapData );
            }
            catch( FormatException ) { }
        }
예제 #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 )
        {
            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 );
        }
예제 #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 )
        {
            if( text == null )
            {
                return base.ConvertFromString( text, row, propertyMapData );
            }

            return new Guid( text );
        }
예제 #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 )
 {
     try
     {
         return Enum.Parse( type, text, true );
     }
     catch
     {
         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.Float;

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

            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 )
        {
            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 );
        }
예제 #7
0
        public void ConvertToStringTest()
        {
            var converter = new ByteConverter();
            var propertyMapData = new CsvPropertyMapData( null )
            {
                TypeConverter = converter,
                TypeConverterOptions = { CultureInfo = CultureInfo.CurrentCulture }
            };

            Assert.AreEqual( "123", converter.ConvertToString( (byte)123, null, propertyMapData ) );

            Assert.AreEqual( "", converter.ConvertToString( null, null, 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 )
        {
            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 );
        }
예제 #9
0
        /// <summary>
        /// Converts the object to a string.
        /// </summary>
        /// <param name="value">The object to convert to a string.</param>
        /// <param name="row">The <see cref="ICsvWriterRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being written.</param>
        /// <returns>The string representation of the object.</returns>
        public virtual string ConvertToString( object value, ICsvWriterRow row, CsvPropertyMapData propertyMapData )
        {
            if( value == null )
            {
                return string.Empty;
            }

            var formattable = value as IFormattable;
            if( formattable != null )
            {
                return formattable.ToString( propertyMapData.TypeConverterOptions.Format, propertyMapData.TypeConverterOptions.CultureInfo );
            }

            return value.ToString();
        }
예제 #10
0
        /// <summary>
        /// Converts the object to a string.
        /// </summary>
        /// <param name="value">The object to convert to a string.</param>
        /// <param name="row">The <see cref="ICsvWriterRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being written.</param>
        /// <returns>The string representation of the object.</returns>
        public override string ConvertToString( object value, ICsvWriterRow row, CsvPropertyMapData propertyMapData )
        {
            var dictionary = value as IDictionary;
            if( dictionary == null )
            {
                return base.ConvertToString( value, row, propertyMapData );
            }

            foreach( DictionaryEntry entry in dictionary )
            {
                row.WriteField( entry.Value );
            }

            return null;
        }
예제 #11
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 );
        }
예제 #12
0
        /// <summary>
        /// Converts the object to a string.
        /// </summary>
        /// <param name="value">The object to convert to a string.</param>
        /// <param name="row"></param>
        /// <param name="propertyMapData"></param>
        /// <returns>The string representation of the object.</returns>
        public override string ConvertToString( object value, ICsvWriterRow row, CsvPropertyMapData propertyMapData )
        {
            var list = value as IEnumerable;
            if( list == null )
            {
                return base.ConvertToString( value, row, propertyMapData );
            }

            foreach( var item in list )
            {
                row.WriteField( item.ToString() );
            }

            return null;
        }
예제 #13
0
        public void ConvertToStringTest()
        {
            var converter = new CharConverter();
            var propertyMapData = new CsvPropertyMapData( null )
            {
                TypeConverter = converter,
                TypeConverterOptions = { CultureInfo = CultureInfo.CurrentCulture }
            };

            Assert.AreEqual( "a", converter.ConvertToString( 'a', null, propertyMapData ) );

            Assert.AreEqual( "True", converter.ConvertToString( true, null, propertyMapData ) );

            Assert.AreEqual( "", converter.ConvertToString( null, null, propertyMapData ) );
        }
예제 #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 )
        {
            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;
        }
예제 #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 )
        {
            if( text == null )
            {
                return string.Empty;
            }

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

            return text;
        }
예제 #16
0
        public void ConvertToStringTest()
        {
            var converter = new DateTimeConverter();
            var propertyMapData = new CsvPropertyMapData( null )
            {
                TypeConverter = converter,
                TypeConverterOptions = { CultureInfo = CultureInfo.CurrentCulture }
            };

            var dateTime = DateTime.Now;

            // Valid conversions.
            Assert.AreEqual( dateTime.ToString(), converter.ConvertToString( dateTime, null, propertyMapData ) );

            // Invalid conversions.
            Assert.AreEqual( "1", converter.ConvertToString( 1, null, propertyMapData ) );
            Assert.AreEqual( "", converter.ConvertToString( null, null, propertyMapData ) );
        }
예제 #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 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;
        }
예제 #18
0
        public void ConvertToStringTest()
        {
            var converter = new TimeSpanConverter();
            var propertyMapData = new CsvPropertyMapData( null )
            {
                TypeConverter = converter,
                TypeConverterOptions = { CultureInfo = CultureInfo.CurrentCulture }
            };

            var dateTime = DateTime.Now;
            var timeSpan = new TimeSpan( dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond );

            // Valid conversions.
            Assert.AreEqual( timeSpan.ToString(), converter.ConvertToString( timeSpan, null, propertyMapData ) );

            // Invalid conversions.
            Assert.AreEqual( "1", converter.ConvertToString( 1, null, propertyMapData ) );
            Assert.AreEqual( "", converter.ConvertToString( null, null, propertyMapData ) );
        }
예제 #19
0
        public void ConvertFromStringTest()
        {
            var converter = new ByteConverter();

            var propertyMapData = new CsvPropertyMapData( null );
            propertyMapData.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            Assert.AreEqual( (byte)123, converter.ConvertFromString( "123", null, propertyMapData ) );
            Assert.AreEqual( (byte)123, converter.ConvertFromString( " 123 ", null, propertyMapData ) );

            try
            {
                converter.ConvertFromString( null, null, propertyMapData );
                Assert.Fail();
            }
            catch( CsvTypeConverterException )
            {
            }
        }
예제 #20
0
        public void ConvertFromStringTest()
        {
            var converter = new BooleanConverter();

            var propertyMapData = new CsvPropertyMapData( null );
            propertyMapData.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            Assert.IsTrue( (bool)converter.ConvertFromString( "true", null, propertyMapData ) );
            Assert.IsTrue( (bool)converter.ConvertFromString( "True", null, propertyMapData ) );
            Assert.IsTrue( (bool)converter.ConvertFromString( "TRUE", null, propertyMapData ) );
            Assert.IsTrue( (bool)converter.ConvertFromString( "1", null, propertyMapData ) );
            Assert.IsTrue( (bool)converter.ConvertFromString( "yes", null, propertyMapData ) );
            Assert.IsTrue( (bool)converter.ConvertFromString( "YES", null, propertyMapData ) );
            Assert.IsTrue( (bool)converter.ConvertFromString( "y", null, propertyMapData ) );
            Assert.IsTrue( (bool)converter.ConvertFromString( "Y", null, propertyMapData ) );
            Assert.IsTrue( (bool)converter.ConvertFromString( " true ", null, propertyMapData ) );
            Assert.IsTrue( (bool)converter.ConvertFromString( " yes ", null, propertyMapData ) );
            Assert.IsTrue( (bool)converter.ConvertFromString( " y ", null, propertyMapData ) );

            Assert.IsFalse( (bool)converter.ConvertFromString( "false", null, propertyMapData ) );
            Assert.IsFalse( (bool)converter.ConvertFromString( "False", null, propertyMapData ) );
            Assert.IsFalse( (bool)converter.ConvertFromString( "FALSE", null, propertyMapData ) );
            Assert.IsFalse( (bool)converter.ConvertFromString( "0", null, propertyMapData ) );
            Assert.IsFalse( (bool)converter.ConvertFromString( "no", null, propertyMapData ) );
            Assert.IsFalse( (bool)converter.ConvertFromString( "NO", null, propertyMapData ) );
            Assert.IsFalse( (bool)converter.ConvertFromString( "n", null, propertyMapData ) );
            Assert.IsFalse( (bool)converter.ConvertFromString( "N", null, propertyMapData ) );
            Assert.IsFalse( (bool)converter.ConvertFromString( " false ", null, propertyMapData ) );
            Assert.IsFalse( (bool)converter.ConvertFromString( " 0 ", null, propertyMapData ) );
            Assert.IsFalse( (bool)converter.ConvertFromString( " no ", null, propertyMapData ) );
            Assert.IsFalse( (bool)converter.ConvertFromString( " n ", null, propertyMapData ) );

            try
            {
                converter.ConvertFromString( null, null, propertyMapData );
                Assert.Fail();
            }
            catch( CsvTypeConverterException )
            {
            }
        }
        /// <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 )
        {
            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;
        }
예제 #23
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 );
        }
        /// <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;
        }
예제 #25
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 void ConvertTest()
        {
            var converter = new EnumerableConverter();

            var propertyMapData = new CsvPropertyMapData( null );
            propertyMapData.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            try
            {
                converter.ConvertFromString( "", null, propertyMapData );
                Assert.Fail();
            }
            catch( CsvTypeConverterException )
            {
            }
            try
            {
                converter.ConvertToString( 5, null, propertyMapData );
                Assert.Fail();
            }
            catch( CsvTypeConverterException )
            {
            }
        }
예제 #27
0
        public void ConvertFromStringTest()
        {
            var converter = new DateTimeConverter();

            var propertyMapData = new CsvPropertyMapData( null );
            propertyMapData.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            var dateTime = DateTime.Now;

            // Valid conversions.
            Assert.AreEqual( dateTime.ToString(), converter.ConvertFromString( dateTime.ToString(), null, propertyMapData ).ToString() );
            Assert.AreEqual( dateTime.ToString(), converter.ConvertFromString( dateTime.ToString( "o" ), null, propertyMapData ).ToString() );
            Assert.AreEqual( dateTime.ToString(), converter.ConvertFromString( " " + dateTime + " ", null, propertyMapData ).ToString() );

            // Invalid conversions.
            try
            {
                converter.ConvertFromString( null, null, propertyMapData );
                Assert.Fail();
            }
            catch( CsvTypeConverterException )
            {
            }
        }
예제 #28
0
 /// <summary>
 /// Converts the object to a string.
 /// </summary>
 /// <param name="value">The object to convert to a string.</param>
 /// <param name="row"></param>
 /// <param name="propertyMapData"></param>
 /// <returns>The string representation of the object.</returns>
 public override string ConvertToString( object value, ICsvWriterRow row, CsvPropertyMapData propertyMapData )
 {
     return UnderlyingTypeConverter.ConvertToString( value, row, propertyMapData );
 }
예제 #29
0
 /// <summary>
 /// Throws an exception.
 /// </summary>
 /// <param name="value">The object to convert to a string.</param>
 /// <param name="row">The <see cref="ICsvWriterRow"/> for the current record.</param>
 /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being written.</param>
 /// <returns>The string representation of the object.</returns>
 public override string ConvertToString( object value, ICsvWriterRow 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." );
 }
예제 #30
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 );
        }