本文整理汇总了C#中CsvHelper.TypeConversion.TypeConverterOptions类的典型用法代码示例。如果您正苦于以下问题:C# TypeConverterOptions类的具体用法?C# TypeConverterOptions怎么用?C# TypeConverterOptions使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TypeConverterOptions类属于CsvHelper.TypeConversion命名空间,在下文中一共展示了TypeConverterOptions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertTo
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;
}
示例2: ConvertTo
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");
}
示例3: ConvertToString
public string ConvertToString(TypeConverterOptions options, object value)
{
var color = (Color)value;
var stringValue = ColorTranslator.ToHtml(color);
return stringValue;
}
示例4: ConvertFromStringTest
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 )
{
}
}
示例5: ConvertFromString
/// <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 );
}
示例6: ConvertTest
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 )
{
}
}
示例7: ConvertFromStringTest
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 )
{
}
}
示例8: Merge
/// <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;
}
示例9: ConvertFromString
/// <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);
}
示例10: ConvertFromString
public object ConvertFromString(TypeConverterOptions options, string text)
{
if (string.IsNullOrWhiteSpace(text))
return new DateTime?();
var value = ParseDate(options, text);
return value;
}
示例11: ConvertFromString
public override object ConvertFromString(TypeConverterOptions options, string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return null;
}
return base.ConvertFromString(options, text);
}
示例12: ConvertFromString
/// <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 );
}
示例13: ConvertFromString
public object ConvertFromString(TypeConverterOptions options, string text)
{
if (string.IsNullOrWhiteSpace(text))
{
text = Resources.DependencyNone;
}
return text;
}
示例14: ConvertFromString
/// <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 );
}
示例15: ConvertFromString
/// <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;
}