本文整理匯總了C#中System.ComponentModel.TypeConverter.ConvertTo方法的典型用法代碼示例。如果您正苦於以下問題:C# TypeConverter.ConvertTo方法的具體用法?C# TypeConverter.ConvertTo怎麽用?C# TypeConverter.ConvertTo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.ComponentModel.TypeConverter
的用法示例。
在下文中一共展示了TypeConverter.ConvertTo方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: GetDateTime
private DateTime GetDateTime(object value, TypeConverter converter)
{
if (typeof(DateTime).IsAssignableFrom(value.GetType()))
return (DateTime) value;
else
return (DateTime) converter.ConvertTo(value, typeof(DateTime));
}
示例2: GetFormattedValue
protected virtual object GetFormattedValue (object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (DataGridView == null)
return null;
if (rowIndex < 0 || rowIndex >= DataGridView.RowCount)
throw new ArgumentOutOfRangeException ("rowIndex");
// Give the user a chance to custom format
if (!(this is DataGridViewRowHeaderCell)) {
DataGridViewCellFormattingEventArgs e = new DataGridViewCellFormattingEventArgs (ColumnIndex, rowIndex, value, FormattedValueType, cellStyle);
DataGridView.OnCellFormattingInternal (e);
if (e.FormattingApplied)
return e.Value;
cellStyle = e.CellStyle;
value = e.Value;
}
if (value == null || (cellStyle != null && value == cellStyle.DataSourceNullValue)) {
if (FormattedValueType == typeof (string))
return String.Empty;
}
if (FormattedValueType == typeof(string) && value is IFormattable && !String.IsNullOrEmpty (cellStyle.Format))
return ((IFormattable) value).ToString (cellStyle.Format, cellStyle.FormatProvider);
if (value != null && FormattedValueType.IsAssignableFrom (value.GetType()))
return value;
if (formattedValueTypeConverter == null)
formattedValueTypeConverter = FormattedValueTypeConverter;
if (valueTypeConverter == null)
valueTypeConverter = ValueTypeConverter;
if (valueTypeConverter != null && valueTypeConverter.CanConvertTo (FormattedValueType))
return valueTypeConverter.ConvertTo (value, FormattedValueType);
if (formattedValueTypeConverter != null && formattedValueTypeConverter.CanConvertFrom (ValueType))
return formattedValueTypeConverter.ConvertFrom (value);
return Convert.ChangeType (value, FormattedValueType);
}
示例3: FormatObjectInternal
/// <devdoc>
///
/// Converts a value into a format suitable for display to the end user.
///
/// - Converts DBNull or null into a suitable formatted representation of 'null'
/// - Performs some special-case conversions (eg. Boolean to CheckState)
/// - Uses TypeConverters or IConvertible where appropriate
/// - Throws a FormatException is no suitable conversion can be found
///
/// </devdoc>
private static object FormatObjectInternal(object value,
Type targetType,
TypeConverter sourceConverter,
TypeConverter targetConverter,
string formatString,
IFormatProvider formatInfo,
object formattedNullValue) {
if (value == System.DBNull.Value || value == null) {
//
// Convert DBNull to the formatted representation of 'null' (if possible)
//
if (formattedNullValue != null)
{
return formattedNullValue;
}
//
// Convert DBNull or null to a specific 'known' representation of null (otherwise fail)
//
if (targetType == stringType)
{
return String.Empty;
}
if (targetType == checkStateType) {
return CheckState.Indeterminate;
}
// Just pass null through: if this is a value type, it's been unwrapped here, so we return null
// and the caller has to wrap if appropriate.
return null;
}
//
// Special case conversions
//
if (targetType == stringType) {
if (value is IFormattable && !String.IsNullOrEmpty(formatString)) {
return (value as IFormattable).ToString(formatString, formatInfo);
}
}
//The converters for properties should take precedence. Unfortunately, we don't know whether we have one. Check vs. the
//type's TypeConverter. We're punting the case where the property-provided converter is the same as the type's converter.
Type sourceType = value.GetType();
TypeConverter sourceTypeTypeConverter = TypeDescriptor.GetConverter(sourceType);
if (sourceConverter != null && sourceConverter != sourceTypeTypeConverter && sourceConverter.CanConvertTo(targetType)) {
return sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType);
}
TypeConverter targetTypeTypeConverter = TypeDescriptor.GetConverter(targetType);
if (targetConverter != null && targetConverter != targetTypeTypeConverter && targetConverter.CanConvertFrom(sourceType)) {
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value);
}
if (targetType == checkStateType) {
if (sourceType == booleanType) {
return ((bool)value) ? CheckState.Checked : CheckState.Unchecked;
}
else {
if (sourceConverter == null) {
sourceConverter = sourceTypeTypeConverter;
}
if (sourceConverter != null && sourceConverter.CanConvertTo(booleanType)) {
return (bool)sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, booleanType)
? CheckState.Checked : CheckState.Unchecked;
}
}
}
if (targetType.IsAssignableFrom(sourceType)) {
return value;
}
//
// If explicit type converters not provided, supply default ones instead
//
if (sourceConverter == null) {
sourceConverter = sourceTypeTypeConverter;
}
if (targetConverter == null) {
targetConverter = targetTypeTypeConverter;
}
//
// Standardized conversions
//
//.........這裏部分代碼省略.........
示例4: ParseFormattedValue
public virtual object ParseFormattedValue (object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
if (cellStyle == null)
throw new ArgumentNullException ("cellStyle is null.");
if (FormattedValueType == null)
throw new FormatException ("The System.Windows.Forms.DataGridViewCell.FormattedValueType property value is null.");
if (formattedValue == null)
throw new ArgumentException ("formattedValue is null.");
if (ValueType == null)
throw new FormatException ("valuetype is null");
if (!FormattedValueType.IsAssignableFrom (formattedValue.GetType ()))
throw new ArgumentException ("formattedValue is not of formattedValueType.");
if (formattedValueTypeConverter == null)
formattedValueTypeConverter = FormattedValueTypeConverter;
if (valueTypeConverter == null)
valueTypeConverter = ValueTypeConverter;
if (valueTypeConverter != null && valueTypeConverter.CanConvertFrom (FormattedValueType))
return valueTypeConverter.ConvertFrom (formattedValue);
if (formattedValueTypeConverter != null && formattedValueTypeConverter.CanConvertTo (ValueType))
return formattedValueTypeConverter.ConvertTo (formattedValue, ValueType);
return Convert.ChangeType (formattedValue, ValueType);
}
示例5: ParseObjectInternal
/// <devdoc>
///
/// Converts a value entered by the end user (through UI) into the corresponding binary value.
///
/// - Converts formatted representations of 'null' into DBNull
/// - Performs some special-case conversions (eg. CheckState to Boolean)
/// - Uses TypeConverters or IConvertible where appropriate
/// - Throws a FormatException is no suitable conversion can be found
///
/// </devdoc>
private static object ParseObjectInternal(object value,
Type targetType,
Type sourceType,
TypeConverter targetConverter,
TypeConverter sourceConverter,
IFormatProvider formatInfo,
object formattedNullValue) {
//
// Convert the formatted representation of 'null' to DBNull (if possible)
//
if (EqualsFormattedNullValue(value, formattedNullValue, formatInfo) || value == System.DBNull.Value) {
return System.DBNull.Value;
}
//
// Special case conversions
//
TypeConverter targetTypeTypeConverter = TypeDescriptor.GetConverter(targetType);
if (targetConverter != null && targetTypeTypeConverter != targetConverter && targetConverter.CanConvertFrom(sourceType)) {
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value);
}
TypeConverter sourceTypeTypeConverter = TypeDescriptor.GetConverter(sourceType);
if (sourceConverter != null && sourceTypeTypeConverter != sourceConverter && sourceConverter.CanConvertTo(targetType)) {
return sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType);
}
if (value is string) {
// If target type has a suitable Parse method, use that to parse strings
object parseResult = InvokeStringParseMethod(value, targetType, formatInfo);
if (parseResult != parseMethodNotFound) {
return parseResult;
}
}
else if (value is CheckState) {
CheckState state = (CheckState)value;
if (state == CheckState.Indeterminate) {
return DBNull.Value;
}
// Explicit conversion from CheckState to Boolean
if (targetType == booleanType) {
return (state == CheckState.Checked);
}
if (targetConverter == null) {
targetConverter = targetTypeTypeConverter;
}
if (targetConverter != null && targetConverter.CanConvertFrom(booleanType)) {
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), state == CheckState.Checked);
}
}
else if (value != null && targetType.IsAssignableFrom(value.GetType())) {
// If value is already of a compatible type, just go ahead and use it
return value;
}
//
// If explicit type converters not provided, supply default ones instead
//
if (targetConverter == null) {
targetConverter = targetTypeTypeConverter;
}
if (sourceConverter == null) {
sourceConverter = sourceTypeTypeConverter;
}
//
// Standardized conversions
//
if (targetConverter != null && targetConverter.CanConvertFrom(sourceType)) {
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value);
}
else if (sourceConverter != null && sourceConverter.CanConvertTo(targetType)) {
return sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType);
}
else if (value is IConvertible) {
return ChangeType(value, targetType, formatInfo);
}
//
// Fail if no suitable conversion found
//
throw new FormatException(GetCantConvertMessage(value, targetType));
}
示例6: FormatObjectInternal
private static object FormatObjectInternal(object value, System.Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, string formatString, IFormatProvider formatInfo, object formattedNullValue)
{
if ((value == DBNull.Value) || (value == null))
{
if (formattedNullValue != null)
{
return formattedNullValue;
}
if (targetType == stringType)
{
return string.Empty;
}
if (targetType == checkStateType)
{
return CheckState.Indeterminate;
}
return null;
}
if (((targetType == stringType) && (value is IFormattable)) && !string.IsNullOrEmpty(formatString))
{
return (value as IFormattable).ToString(formatString, formatInfo);
}
System.Type type = value.GetType();
TypeConverter converter = TypeDescriptor.GetConverter(type);
if (((sourceConverter != null) && (sourceConverter != converter)) && sourceConverter.CanConvertTo(targetType))
{
return sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType);
}
TypeConverter converter2 = TypeDescriptor.GetConverter(targetType);
if (((targetConverter != null) && (targetConverter != converter2)) && targetConverter.CanConvertFrom(type))
{
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value);
}
if (targetType == checkStateType)
{
if (type == booleanType)
{
return (((bool) value) ? CheckState.Checked : CheckState.Unchecked);
}
if (sourceConverter == null)
{
sourceConverter = converter;
}
if ((sourceConverter != null) && sourceConverter.CanConvertTo(booleanType))
{
return (((bool) sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, booleanType)) ? CheckState.Checked : CheckState.Unchecked);
}
}
if (targetType.IsAssignableFrom(type))
{
return value;
}
if (sourceConverter == null)
{
sourceConverter = converter;
}
if (targetConverter == null)
{
targetConverter = converter2;
}
if ((sourceConverter != null) && sourceConverter.CanConvertTo(targetType))
{
return sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType);
}
if ((targetConverter != null) && targetConverter.CanConvertFrom(type))
{
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value);
}
if (!(value is IConvertible))
{
throw new FormatException(GetCantConvertMessage(value, targetType));
}
return ChangeType(value, targetType, formatInfo);
}
示例7: ParseObjectInternal
private static object ParseObjectInternal(object value, System.Type targetType, System.Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, object formattedNullValue)
{
if (EqualsFormattedNullValue(value, formattedNullValue, formatInfo) || (value == DBNull.Value))
{
return DBNull.Value;
}
TypeConverter converter = TypeDescriptor.GetConverter(targetType);
if (((targetConverter != null) && (converter != targetConverter)) && targetConverter.CanConvertFrom(sourceType))
{
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value);
}
TypeConverter converter2 = TypeDescriptor.GetConverter(sourceType);
if (((sourceConverter != null) && (converter2 != sourceConverter)) && sourceConverter.CanConvertTo(targetType))
{
return sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType);
}
if (value is string)
{
object obj2 = InvokeStringParseMethod(value, targetType, formatInfo);
if (obj2 != parseMethodNotFound)
{
return obj2;
}
}
else if (value is CheckState)
{
CheckState state = (CheckState) value;
if (state == CheckState.Indeterminate)
{
return DBNull.Value;
}
if (targetType == booleanType)
{
return (state == CheckState.Checked);
}
if (targetConverter == null)
{
targetConverter = converter;
}
if ((targetConverter != null) && targetConverter.CanConvertFrom(booleanType))
{
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), state == CheckState.Checked);
}
}
else if ((value != null) && targetType.IsAssignableFrom(value.GetType()))
{
return value;
}
if (targetConverter == null)
{
targetConverter = converter;
}
if (sourceConverter == null)
{
sourceConverter = converter2;
}
if ((targetConverter != null) && targetConverter.CanConvertFrom(sourceType))
{
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value);
}
if ((sourceConverter != null) && sourceConverter.CanConvertTo(targetType))
{
return sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType);
}
if (!(value is IConvertible))
{
throw new FormatException(GetCantConvertMessage(value, targetType));
}
return ChangeType(value, targetType, formatInfo);
}
示例8: CheckConversion
public void CheckConversion(object check, object expect, TypeConverter conv, Type type, bool test_from, bool test_to, CultureInfo culture) {
object obj;
object result;
obj = check;
if (debug > 0) {
Console.WriteLine("{0}: CheckConversion, checking {1}({2}) <-> {3}({4})", conv.ToString(), check.GetType().ToString(), check.ToString(), type.ToString(), expect != null ? expect.ToString() : "null");
}
if (test_to) {
obj = conv.ConvertTo(null, culture, check, type);
if (obj == null) {
if (expect != null) {
failed++;
Console.WriteLine("{0}: ConvertTo failed, type {1}, expected {2}, got null", conv.ToString(), type.ToString(), expect.ToString());
}
return;
}
// Intermediate verification
if (expect != null && !obj.Equals(expect)) {
failed++;
if (verbose > 0) {
Console.WriteLine("{0}: ConvertTo failed, type {1}, expected {2}, got {3}", conv.ToString(), type.ToString(), expect.ToString(), obj.ToString());
}
}
if (debug > 1) {
Console.WriteLine("{0}: CheckConversion, ConvertTo result: '{1}')", conv.ToString(), obj);
}
}
if (test_from) {
result = conv.ConvertFrom(null, culture, obj);
if (test_to) {
// Roundtrip check
if (!check.Equals(result)) {
failed++;
if (verbose > 0) {
Console.WriteLine("{0}: ConvertTo/ConvertFrom roundtrip failed, type {1}", conv.ToString(), type.ToString());
}
}
} else {
if (!expect.Equals(result)) {
failed++;
if (verbose > 0) {
Console.WriteLine("{0}: ConvertFrom failed, type {1}", conv.ToString(), type.ToString());
}
}
}
if (debug > 1) {
Console.WriteLine("{0}: CheckConversion, ConvertFrom result: '{1}')", conv.ToString(), result);
}
}
}
示例9: TryConvertTo
private static bool TryConvertTo( object value, Type targetType, CultureInfo culture, TypeConverter converter, out object result )
{
if( converter != null )
{
try
{
result = converter.ConvertTo( null, culture, value, targetType );
return true;
}
catch
{
// We'll try to convert the value another way.
}
if( ( value != null ) && ( converter.CanConvertFrom( value.GetType() ) ) )
{
try
{
var newValue = converter.ConvertFrom( null, culture, value );
result = converter.ConvertTo( null, culture, newValue, targetType );
return true;
}
catch
{
}
}
}
result = null;
return false;
}