protected override string ConvertTo(TypeConverterOptions options, object value) { if (value == null) return "Unknown"; var dt = Convert.ToDateTime(value); if (dt == DateTime.MinValue) return "Unknown"; return dt.ToString("g"); }
public void ConvertFromStringTest() { var converter = new DateTimeConverter(); var typeConverterOptions = new TypeConverterOptions { CultureInfo = CultureInfo.CurrentCulture }; var dateTime = DateTime.Now; // Valid conversions. Assert.AreEqual( dateTime.ToString(), converter.ConvertFromString( typeConverterOptions, dateTime.ToString() ).ToString() ); Assert.AreEqual( dateTime.ToString(), converter.ConvertFromString( typeConverterOptions, dateTime.ToString( "o" ) ).ToString() ); Assert.AreEqual( dateTime.ToString(), converter.ConvertFromString( typeConverterOptions, " " + dateTime + " " ).ToString() ); // Invalid conversions. try { converter.ConvertFromString( typeConverterOptions, null ); Assert.Fail(); } catch( CsvTypeConverterException ) { } }
/// <summary> /// Merges TypeConverterOptions by applying the values of sources in order to a /// new TypeConverterOptions instance. /// </summary> /// <param name="sources">The sources that will be applied.</param> /// <returns>A new instance of TypeConverterOptions with the source applied to it.</returns> public static TypeConverterOptions Merge( params TypeConverterOptions[] sources ) { var options = new TypeConverterOptions(); foreach( var source in sources ) { if( source == null ) { continue; } if( source.CultureInfo != null ) { options.CultureInfo = source.CultureInfo; } if( source.DateTimeStyle != null ) { options.DateTimeStyle = source.DateTimeStyle; } #if !NET_2_0 && !NET_3_5 && !PCL if( source.TimeSpanStyle != null ) { options.TimeSpanStyle = source.TimeSpanStyle; } #endif if( source.NumberStyle != null ) { options.NumberStyle = source.NumberStyle; } if( source.Format != null ) { options.Format = source.Format; } #if NET_2_0 if( !EnumerableHelper.SequenceEqual( options.booleanTrueValues, source.booleanTrueValues ) ) #else if( !options.booleanTrueValues.SequenceEqual( source.booleanTrueValues ) ) #endif { options.booleanTrueValues.Clear(); options.booleanTrueValues.AddRange( source.booleanTrueValues ); } #if NET_2_0 if( !EnumerableHelper.SequenceEqual( options.booleanFalseValues, source.booleanFalseValues ) ) #else if( !options.booleanFalseValues.SequenceEqual( source.booleanFalseValues ) ) #endif { options.booleanFalseValues.Clear(); options.booleanFalseValues.AddRange( source.booleanFalseValues ); } } return options; }
protected override string ConvertTo(TypeConverterOptions options, object value) { double d = Convert.ToDouble(value); string format = options.NumberStyle.HasValue ? "#.#" : "###.#"; string s = d.IsZero() ? "Unknown" : d.ToString(format); return s; }
public string ConvertToString(TypeConverterOptions options, object value) { var color = (Color)value; var stringValue = ColorTranslator.ToHtml(color); return stringValue; }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString( TypeConverterOptions options, string text ) { var formatProvider = (IFormatProvider)options.CultureInfo; TimeSpan span; #if !NET_2_0 && !NET_3_5 && !PCL var timeSpanStyle = options.TimeSpanStyle ?? TimeSpanStyles.None; if( !string.IsNullOrEmpty( options.Format ) && TimeSpan.TryParseExact( text, options.Format, formatProvider, timeSpanStyle, out span ) ) { return span; } if( string.IsNullOrEmpty( options.Format ) && TimeSpan.TryParse( text, formatProvider, out span ) ) { return span; } #else if( TimeSpan.TryParse( text, out span ) ) { return span; } #endif return base.ConvertFromString( options, text ); }
public void ConvertTest() { var converter = new EnumerableConverter(); var typeConverterOptions = new TypeConverterOptions { CultureInfo = CultureInfo.CurrentCulture }; Assert.IsTrue( converter.CanConvertFrom( typeof( string ) ) ); Assert.IsTrue( converter.CanConvertTo( typeof( string ) ) ); try { converter.ConvertFromString( typeConverterOptions, "" ); Assert.Fail(); } catch( CsvTypeConverterException ) { } try { converter.ConvertToString( typeConverterOptions, 5 ); Assert.Fail(); } catch( CsvTypeConverterException ) { } }
public void ConvertFromStringTest() { var converter = new EnumConverter( typeof( TestEnum ) ); var typeConverterOptions = new TypeConverterOptions { CultureInfo = CultureInfo.CurrentCulture }; Assert.AreEqual( TestEnum.One, converter.ConvertFromString( typeConverterOptions, "One" ) ); Assert.AreEqual( TestEnum.One, converter.ConvertFromString( typeConverterOptions, "one" ) ); Assert.AreEqual( TestEnum.One, converter.ConvertFromString( typeConverterOptions, "1" ) ); try { Assert.AreEqual( TestEnum.One, converter.ConvertFromString( typeConverterOptions, "" ) ); Assert.Fail(); } catch( CsvTypeConverterException ) { } try { Assert.AreEqual( TestEnum.One, converter.ConvertFromString( typeConverterOptions, null ) ); Assert.Fail(); } catch( CsvTypeConverterException ) { } }
public object ConvertFromString(TypeConverterOptions options, string text) { if (string.IsNullOrWhiteSpace(text)) return new DateTime?(); var value = ParseDate(options, text); return value; }
public override object ConvertFromString(TypeConverterOptions options, string text) { if (string.IsNullOrWhiteSpace(text)) { return null; } return base.ConvertFromString(options, text); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns> /// The object created from the string. /// </returns> public override object ConvertFromString(TypeConverterOptions options, string text) { var numberStyle = options.NumberStyle; var style = numberStyle.HasValue ? numberStyle.GetValueOrDefault() : NumberStyles.Float; double result; if (double.TryParse(text, style, options.CultureInfo, out result)) return Length.FromMillimeters(result); return base.ConvertFromString(options, text); }
public object ConvertFromString(TypeConverterOptions options, string text) { if (string.IsNullOrWhiteSpace(text)) { text = Resources.DependencyNone; } return text; }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString( TypeConverterOptions options, string text ) { if( text == null ) { return base.ConvertFromString( options, text ); } return new Guid( text ); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString( TypeConverterOptions options, string text ) { if( string.IsNullOrEmpty( text ) ) { return null; } return UnderlyingTypeConverter.ConvertFromString( options, text ); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString( TypeConverterOptions options, string text ) { if( text == null ) { return string.Empty; } return text; }
public string ConvertToString(TypeConverterOptions options, object value) { if (value == null) return null; var dateValue = (DateTime) value; var formattedDate = dateValue.ToString("yyyyMMdd"); return formattedDate; }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString( TypeConverterOptions options, string text ) { try { return Enum.Parse( type, text + "some unneeded string here", true ); } catch { return base.ConvertFromString( options, text ); } }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString( TypeConverterOptions options, string text ) { TimeSpan span; if( TimeSpan.TryParse( text, out span ) ) { return span; } return base.ConvertFromString( options, text ); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString( TypeConverterOptions options, string text ) { var numberStyle = options.NumberStyle ?? NumberStyles.Integer; uint ui; if( uint.TryParse( text, numberStyle, options.CultureInfo, out ui ) ) { return ui; } return base.ConvertFromString( options, text ); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString( TypeConverterOptions options, string text ) { var numberStyle = options.NumberStyle ?? NumberStyles.Float; float f; if( float.TryParse( text, numberStyle, options.CultureInfo, out f ) ) { return f; } return base.ConvertFromString( options, text ); }
public void ConvertToStringTest() { var converter = new ByteConverter(); var typeConverterOptions = new TypeConverterOptions { CultureInfo = CultureInfo.CurrentCulture }; Assert.AreEqual( "123", converter.ConvertToString( typeConverterOptions, (byte)123 ) ); Assert.AreEqual( "", converter.ConvertToString( typeConverterOptions, null ) ); }
protected override object ConvertFrom(TypeConverterOptions options, string text) { if (text.IsNullOrWhitespace() || text.EqualIgnoreCase("Unknown")) { return 0d; } double d; if (!double.TryParse(text, out d)) { return 0; } return d; }
public void ConvertToStringTest() { var converter = new CharConverter(); var typeConverterOptions = new TypeConverterOptions { CultureInfo = CultureInfo.CurrentCulture }; Assert.AreEqual( "a", converter.ConvertToString( typeConverterOptions, 'a' ) ); Assert.AreEqual( "True", converter.ConvertToString( typeConverterOptions, true ) ); Assert.AreEqual( "", converter.ConvertToString( typeConverterOptions, null ) ); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString(TypeConverterOptions options, string text) { var formatProvider = (IFormatProvider)options.CultureInfo.GetFormat(typeof(DateTimeFormatInfo)) ?? options.CultureInfo; TimeSpan span; if(TimeSpan.TryParse(text,formatProvider,out span)) { return span; } return base.ConvertFromString(options, text); }
public BeaconEventsUtils (SQLiteConnection sqlConnection, INotificationService notificationService) { _sqlConnection = sqlConnection; _notificationService = notificationService; sqlConnection.CreateTable<BeaconEvent> (); var options = new TypeConverterOptions { Format = "MM/d/yyyy HH:mm:ss tz", }; TypeConverterOptionsFactory.AddOptions<DateTime>(options); }
private static DateTime ParseDate(TypeConverterOptions options, string text) { DateTime value; if (options != null && options.DateTimeStyle.HasValue) { value = DateTime.ParseExact(text, "yyyyMMdd", CultureInfo.InvariantCulture, options.DateTimeStyle.Value); } else { value = DateTime.ParseExact(text, "yyyyMMdd", CultureInfo.InvariantCulture); } return value; }
public void ConvertToStringTest() { var converter = new EnumConverter( typeof( TestEnum ) ); var typeConverterOptions = new TypeConverterOptions { CultureInfo = CultureInfo.CurrentCulture }; Assert.AreEqual( "None", converter.ConvertToString( typeConverterOptions, (TestEnum)0 ) ); Assert.AreEqual( "None", converter.ConvertToString( typeConverterOptions, TestEnum.None ) ); Assert.AreEqual( "One", converter.ConvertToString( typeConverterOptions, (TestEnum)1 ) ); Assert.AreEqual( "One", converter.ConvertToString( typeConverterOptions, TestEnum.One ) ); Assert.AreEqual( "", converter.ConvertToString( typeConverterOptions, null ) ); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString( TypeConverterOptions options, string text ) { var formatProvider = (IFormatProvider)options.CultureInfo.GetFormat( typeof( DateTimeFormatInfo ) ) ?? options.CultureInfo; var dateTimeStyle = options.DateTimeStyle ?? DateTimeStyles.None; DateTime dt; if( DateTime.TryParse( text, formatProvider, dateTimeStyle, out dt ) ) { return dt; } return base.ConvertFromString( options, text ); }
public void ConvertToStringTest() { var converter = new BooleanConverter(); var typeConverterOptions = new TypeConverterOptions { CultureInfo = CultureInfo.CurrentCulture }; Assert.AreEqual( "True", converter.ConvertToString( typeConverterOptions, true ) ); Assert.AreEqual( "False", converter.ConvertToString( typeConverterOptions, false ) ); Assert.AreEqual( "", converter.ConvertToString( typeConverterOptions, null ) ); Assert.AreEqual( "1", converter.ConvertToString( typeConverterOptions, 1 ) ); }
/// <summary> /// Converts the object to a string. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="value">The object to convert to a string.</param> /// <returns>The string representation of the object.</returns> public virtual string ConvertToString( TypeConverterOptions options, object value ) { if( value == null ) { return string.Empty; } var formattable = value as IFormattable; if( formattable != null ) { return formattable.ToString( options.Format, options.CultureInfo ); } return value.ToString(); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString(TypeConverterOptions options, string text) { if (text == null) { return(base.ConvertFromString(options, null)); } if (text.Trim().Length == 0) { return(DateTimeOffset.MinValue); } var formatProvider = (IFormatProvider)options.CultureInfo.GetFormat(typeof(DateTimeFormatInfo)) ?? options.CultureInfo; var dateTimeStyle = options.DateTimeStyle ?? DateTimeStyles.None; return(string.IsNullOrEmpty(options.Format) ? DateTimeOffset.Parse(text, formatProvider, dateTimeStyle) : DateTimeOffset.ParseExact(text, options.Format, formatProvider, dateTimeStyle)); }
/// <summary> /// Get the <see cref="TypeConverterOptions"/> for the given <see cref="Type"/>. /// </summary> /// <param name="type">The type the options are for.</param> /// <returns>The options for the given type.</returns> public TypeConverterOptions GetOptions(Type type) { if (type == null) { throw new ArgumentNullException(); } TypeConverterOptions options; lock ( locker ) { if (!typeConverterOptions.TryGetValue(type, out options)) { options = new TypeConverterOptions(); typeConverterOptions.Add(type, options); } } return(options); }
/// <summary> /// Get the <see cref="T:CsvHelper.TypeConversion.TypeConverterOptions" /> for the given <see cref="T:System.Type" />. /// </summary> /// <param name="type">The type the options are for.</param> /// <returns>The options for the given type.</returns> public static TypeConverterOptions GetOptions(Type type) { TypeConverterOptions typeConverterOption; TypeConverterOptions typeConverterOption1; if (type == null) { throw new ArgumentNullException(); } lock (locker) { if (!typeConverterOptions.TryGetValue(type, out typeConverterOption)) { typeConverterOption = new TypeConverterOptions(); typeConverterOptions.Add(type, typeConverterOption); } typeConverterOption1 = typeConverterOption; } return(typeConverterOption1); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString(TypeConverterOptions options, string text) { if (text == null) { return(base.ConvertFromString(options, null)); } if (text.Trim().Length == 0) { return(DateTime.MinValue); } object format = (IFormatProvider)options.CultureInfo.GetFormat(typeof(DateTimeFormatInfo)); if (format == null) { format = options.CultureInfo; } var formatProvider = (IFormatProvider)format; var dateTimeStyle = options.DateTimeStyle; var dateTimeStyle1 = dateTimeStyle.HasValue ? dateTimeStyle.GetValueOrDefault() : DateTimeStyles.None; return(string.IsNullOrEmpty(options.Format) ? DateTime.Parse(text, formatProvider, dateTimeStyle1) : DateTime.ParseExact(text, options.Format, formatProvider, dateTimeStyle1)); }
/// <summary> /// Converts the object to a string. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="value">The object to convert to a string.</param> /// <returns>The string representation of the object.</returns> public override string ConvertToString(TypeConverterOptions options, object value) { return(UnderlyingTypeConverter.ConvertToString(options, value)); }
/// <summary> /// Adds the <see cref="TypeConverterOptions"/> for the given <see cref="Type"/>. /// </summary> /// <typeparam name="T">The type the options are for.</typeparam> /// <param name="options">The options.</param> public void AddOptions <T>(TypeConverterOptions options) { AddOptions(typeof(T), options); }
/// <summary> /// Merges TypeConverterOptions by applying the values of sources in order to a /// new TypeConverterOptions instance. /// </summary> /// <param name="sources">The sources that will be applied.</param> /// <returns>A new instance of TypeConverterOptions with the source applied to it.</returns> public static TypeConverterOptions Merge(params TypeConverterOptions[] sources) { var options = new TypeConverterOptions(); foreach (var source in sources) { if (source == null) { continue; } if (source.CultureInfo != null) { options.CultureInfo = source.CultureInfo; } if (source.DateTimeStyle != null) { options.DateTimeStyle = source.DateTimeStyle; } #if !NET_2_0 && !NET_3_5 && !PCL if (source.TimeSpanStyle != null) { options.TimeSpanStyle = source.TimeSpanStyle; } #endif if (source.NumberStyle != null) { options.NumberStyle = source.NumberStyle; } if (source.Format != null) { options.Format = source.Format; } // Only change the values if they are different than the defaults. // This means there were explicit changes made to the options. if (!defaultBooleanTrueValues.SequenceEqual(source.BooleanTrueValues)) { options.BooleanTrueValues.Clear(); options.BooleanTrueValues.AddRange(source.BooleanTrueValues); } if (!defaultBooleanFalseValues.SequenceEqual(source.BooleanFalseValues)) { options.BooleanFalseValues.Clear(); options.BooleanFalseValues.AddRange(source.BooleanFalseValues); } if (!defaultNullValues.SequenceEqual(source.NullValues)) { options.NullValues.Clear(); options.NullValues.AddRange(source.NullValues); } } return(options); }
/// <summary> /// Throws an exception. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString(TypeConverterOptions options, string text) { 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."); }
public string ConvertToString(CsvHelperTypeConversion.TypeConverterOptions options, object value) { return(converter.ConvertToString(value)); }
public object ConvertFromString(CsvHelperTypeConversion.TypeConverterOptions options, string text) { return(converter.ConvertFromString(text)); }
/// <summary> /// Merges TypeConverterOptions by applying the values of sources in order to a /// new TypeConverterOptions instance. /// </summary> /// <param name="sources">The sources that will be applied.</param> /// <returns>A new instance of TypeConverterOptions with the source applied to it.</returns> public static TypeConverterOptions Merge(params TypeConverterOptions[] sources) { var options = new TypeConverterOptions(); foreach (var source in sources) { if (source == null) { continue; } if (source.CultureInfo != null) { options.CultureInfo = source.CultureInfo; } if (source.DateTimeStyle != null) { options.DateTimeStyle = source.DateTimeStyle; } #if !NET_2_0 && !NET_3_5 && !PCL if (source.TimeSpanStyle != null) { options.TimeSpanStyle = source.TimeSpanStyle; } #endif if (source.NumberStyle != null) { options.NumberStyle = source.NumberStyle; } if (source.Format != null) { options.Format = source.Format; } #if NET_2_0 if (!EnumerableHelper.SequenceEqual(options.booleanTrueValues, source.booleanTrueValues)) #else if (!options.booleanTrueValues.SequenceEqual(source.booleanTrueValues)) #endif { options.booleanTrueValues.Clear(); options.booleanTrueValues.AddRange(source.booleanTrueValues); } #if NET_2_0 if (!EnumerableHelper.SequenceEqual(options.booleanFalseValues, source.booleanFalseValues)) #else if (!options.booleanFalseValues.SequenceEqual(source.booleanFalseValues)) #endif { options.booleanFalseValues.Clear(); options.booleanFalseValues.AddRange(source.booleanFalseValues); } } return(options); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public virtual object ConvertFromString(TypeConverterOptions options, string text) { throw new CsvTypeConverterException("The conversion cannot be performed."); }
/// <summary> /// Throws an exception. /// </summary> /// <param name="options">The options to use when converting.</param> /// <param name="value">The object to convert to a string.</param> /// <returns>The string representation of the object.</returns> public override string ConvertToString(TypeConverterOptions options, object value) { 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."); }