ConvertUtilities Class
Provides utility methods for converting values between types with more flexibility than System.Convert.
public static class ConvertUtilitiesExample
// Using TryChangeType
Assert.True(ConvertUtilities.TryChangeType("42", CultureInfo.InvariantCulture, out int value));
Assert.Equal(42, value);
// Using ChangeType with default value
var result = ConvertUtilities.ChangeType<int>("invalid", defaultValue: 0, CultureInfo.InvariantCulture);
Assert.Equal(0, result);
// Convert with culture-specific formatting
var cultureInfo = CultureInfo.GetCultureInfo("fr-FR");
var number = ConvertUtilities.ChangeType<decimal>("1234,56", defaultValue: 0m, provider: cultureInfo);
Assert.Equal(1234.56m, number);Properties
| Name | Description |
|---|---|
| DefaultConverter | Gets the default converter instance used by the utility methods. |
DefaultConverter
ConvertUtilities.csGets the default converter instance used by the utility methods.
public static IConverter DefaultConverter { get; }Property value
Methods
| Name | Description |
|---|---|
| ChangeType(IConverter, object?, Type) | Converts an input value to a specified type using a custom converter, or returns null if conversion fails. |
| ChangeType(IConverter, object?, Type, object?) | Converts an input value to a specified type using a custom converter, or returns a default value if conversion fails. |
| ChangeType(IConverter, object?, Type, object?, IFormatProvider?) | Converts an input value to a specified type using a custom converter, or returns a default value if conversion fails. |
| ChangeType(object?, Type) | Converts an input value to a specified type using the default converter, or returns null if conversion fails. |
| ChangeType(object?, Type, object?) | Converts an input value to a specified type using the default converter, or returns a default value if conversion fails. |
| ChangeType(object?, Type, object?, IFormatProvider?) | Converts an input value to a specified type using the default converter, or returns a default value if conversion fails. |
| ChangeType<T>(IConverter, object?) | Converts an input value to the specified type using a custom converter, or returns the default value of the type if conversion fails. |
| ChangeType<T>(IConverter, object?, T) | Converts an input value to the specified type using a custom converter, or returns a default value if conversion fails. |
| ChangeType<T>(IConverter, object?, T, IFormatProvider?) | Converts an input value to the specified type using a custom converter, or returns a default value if conversion fails. |
| ChangeType<T>(object?) | Converts an input value to the specified type using the default converter, or returns the default value of the type if conversion fails. |
| ChangeType<T>(object?, T) | Converts an input value to the specified type using the default converter, or returns a default value if conversion fails. |
| ChangeType<T>(object?, T, IFormatProvider?) | Converts an input value to the specified type using the default converter, or returns a default value if conversion fails. |
| TryChangeType(IConverter, object?, Type, IFormatProvider?, object?) | Attempts to convert an input value to a specified type using a custom converter. |
| TryChangeType(IConverter, object?, Type, object?) | Attempts to convert an input value to a specified type using a custom converter and invariant culture. |
| TryChangeType(object?, Type, IFormatProvider?, object?) | Attempts to convert an input value to a specified type using the default converter. |
| TryChangeType(object?, Type, object?) | Attempts to convert an input value to a specified type using the default converter and invariant culture. |
| TryChangeType<T>(IConverter, object?, IFormatProvider?, T) | Attempts to convert an input value to the specified type using a custom converter. |
| TryChangeType<T>(IConverter, object?, T) | Attempts to convert an input value to the specified type using a custom converter and invariant culture. |
| TryChangeType<T>(object?, IFormatProvider?, T) | Attempts to convert an input value to the specified type using the default converter. |
| TryChangeType<T>(object?, T) | Attempts to convert an input value to the specified type using the default converter and invariant culture. |
ChangeType overloads
ChangeType(IConverter, object?, Type)
ConvertUtilities.csConverts an input value to a specified type using a custom converter, or returns null if conversion fails.
public static object? ChangeType(this IConverter converter, object? input, Type conversionType)Parameters
converterIConverter- The converter to use for the conversion.
inputobject?- The value to convert.
conversionTypeType- The type to convert to.
Returns
object? The converted value if conversion succeeded; otherwise, null.
ChangeType(IConverter, object?, Type, object?)
ConvertUtilities.csConverts an input value to a specified type using a custom converter, or returns a default value if conversion fails.
public static object? ChangeType(this IConverter converter, object? input, Type conversionType, object? defaultValue)Parameters
converterIConverter- The converter to use for the conversion.
inputobject?- The value to convert.
conversionTypeType- The type to convert to.
defaultValueobject?- The value to return if conversion fails.
Returns
object? The converted value if conversion succeeded; otherwise, defaultValue.
ChangeType(IConverter, object?, Type, object?, IFormatProvider?)
ConvertUtilities.csConverts an input value to a specified type using a custom converter, or returns a default value if conversion fails.
public static object? ChangeType(this IConverter converter, object? input, Type conversionType, object? defaultValue, IFormatProvider? provider)Parameters
converterIConverter- The converter to use for the conversion.
inputobject?- The value to convert.
conversionTypeType- The type to convert to.
defaultValueobject?- The value to return if conversion fails.
providerIFormatProvider?- An object that provides culture-specific formatting information.
Returns
object? The converted value if conversion succeeded; otherwise, defaultValue.
ChangeType(object?, Type)
ConvertUtilities.csConverts an input value to a specified type using the default converter, or returns null if conversion fails.
ChangeType(object?, Type, object?)
ConvertUtilities.csConverts an input value to a specified type using the default converter, or returns a default value if conversion fails.
ChangeType(object?, Type, object?, IFormatProvider?)
ConvertUtilities.csConverts an input value to a specified type using the default converter, or returns a default value if conversion fails.
public static object? ChangeType(object? input, Type conversionType, object? defaultValue, IFormatProvider? provider)Parameters
inputobject?- The value to convert.
conversionTypeType- The type to convert to.
defaultValueobject?- The value to return if conversion fails.
providerIFormatProvider?- An object that provides culture-specific formatting information.
Returns
object? The converted value if conversion succeeded; otherwise, defaultValue.
ChangeType<T>(IConverter, object?)
ConvertUtilities.csConverts an input value to the specified type using a custom converter, or returns the default value of the type if conversion fails.
public static T? ChangeType<T>(this IConverter converter, object? input)Type parameters
T- The target type to convert to.
Parameters
converterIConverter- The converter to use for the conversion.
inputobject?- The value to convert.
Returns
T? The converted value if conversion succeeded; otherwise, the default value of T.
ChangeType<T>(IConverter, object?, T)
ConvertUtilities.csConverts an input value to the specified type using a custom converter, or returns a default value if conversion fails.
public static T? ChangeType<T>(this IConverter converter, object? input, T defaultValue)Type parameters
T- The target type to convert to.
Parameters
converterIConverter- The converter to use for the conversion.
inputobject?- The value to convert.
defaultValueT- The value to return if conversion fails.
Returns
T? The converted value if conversion succeeded; otherwise, defaultValue.
ChangeType<T>(IConverter, object?, T, IFormatProvider?)
ConvertUtilities.csConverts an input value to the specified type using a custom converter, or returns a default value if conversion fails.
public static T? ChangeType<T>(this IConverter converter, object? input, T defaultValue, IFormatProvider? provider)Type parameters
T- The target type to convert to.
Parameters
converterIConverter- The converter to use for the conversion.
inputobject?- The value to convert.
defaultValueT- The value to return if conversion fails.
providerIFormatProvider?- An object that provides culture-specific formatting information.
Returns
T? The converted value if conversion succeeded; otherwise, defaultValue.
ChangeType<T>(object?)
ConvertUtilities.csConverts an input value to the specified type using the default converter, or returns the default value of the type if conversion fails.
public static T? ChangeType<T>(object? input)Type parameters
T- The target type to convert to.
Parameters
inputobject?- The value to convert.
Returns
T? The converted value if conversion succeeded; otherwise, the default value of T.
ChangeType<T>(object?, T)
ConvertUtilities.csConverts an input value to the specified type using the default converter, or returns a default value if conversion fails.
public static T? ChangeType<T>(object? input, T defaultValue)Type parameters
T- The target type to convert to.
Parameters
inputobject?- The value to convert.
defaultValueT- The value to return if conversion fails.
Returns
T? The converted value if conversion succeeded; otherwise, defaultValue.
ChangeType<T>(object?, T, IFormatProvider?)
ConvertUtilities.csConverts an input value to the specified type using the default converter, or returns a default value if conversion fails.
public static T? ChangeType<T>(object? input, T defaultValue, IFormatProvider? provider)Type parameters
T- The target type to convert to.
Parameters
inputobject?- The value to convert.
defaultValueT- The value to return if conversion fails.
providerIFormatProvider?- An object that provides culture-specific formatting information.
Returns
T? The converted value if conversion succeeded; otherwise, defaultValue.
TryChangeType overloads
TryChangeType(IConverter, object?, Type, IFormatProvider?, object?)
ConvertUtilities.csAttempts to convert an input value to a specified type using a custom converter.
public static bool TryChangeType(this IConverter converter, object? input, Type conversionType, IFormatProvider? provider, out object? value)Parameters
converterIConverter- The converter to use for the conversion.
inputobject?- The value to convert.
conversionTypeType- The type to convert to.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valueobject?- When this method returns, contains the converted value if the conversion succeeded, or null if the conversion failed.
Returns
bool true if the conversion succeeded; otherwise, false.
TryChangeType(IConverter, object?, Type, object?)
ConvertUtilities.csAttempts to convert an input value to a specified type using a custom converter and invariant culture.
public static bool TryChangeType(this IConverter converter, object? input, Type conversionType, out object? value)Parameters
converterIConverter- The converter to use for the conversion.
inputobject?- The value to convert.
conversionTypeType- The type to convert to.
valueobject?- When this method returns, contains the converted value if the conversion succeeded, or null if the conversion failed.
Returns
bool true if the conversion succeeded; otherwise, false.
TryChangeType(object?, Type, IFormatProvider?, object?)
ConvertUtilities.csAttempts to convert an input value to a specified type using the default converter.
public static bool TryChangeType(object? input, Type conversionType, IFormatProvider? provider, out object? value)Parameters
inputobject?- The value to convert.
conversionTypeType- The type to convert to.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valueobject?- When this method returns, contains the converted value if the conversion succeeded, or null if the conversion failed.
Returns
bool true if the conversion succeeded; otherwise, false.
TryChangeType(object?, Type, object?)
ConvertUtilities.csAttempts to convert an input value to a specified type using the default converter and invariant culture.
TryChangeType<T>(IConverter, object?, IFormatProvider?, T)
ConvertUtilities.csAttempts to convert an input value to the specified type using a custom converter.
public static bool TryChangeType<T>(this IConverter converter, object? input, IFormatProvider? provider, out T value)Type parameters
T- The target type to convert to.
Parameters
converterIConverter- The converter to use for the conversion.
inputobject?- The value to convert.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valueT- When this method returns, contains the converted value if the conversion succeeded, or the default value if the conversion failed.
Returns
bool true if the conversion succeeded; otherwise, false.
TryChangeType<T>(IConverter, object?, T)
ConvertUtilities.csAttempts to convert an input value to the specified type using a custom converter and invariant culture.
public static bool TryChangeType<T>(this IConverter converter, object? input, out T value)Type parameters
T- The target type to convert to.
Parameters
converterIConverter- The converter to use for the conversion.
inputobject?- The value to convert.
valueT- When this method returns, contains the converted value if the conversion succeeded, or the default value if the conversion failed.
Returns
bool true if the conversion succeeded; otherwise, false.
TryChangeType<T>(object?, IFormatProvider?, T)
ConvertUtilities.csAttempts to convert an input value to the specified type using the default converter.
public static bool TryChangeType<T>(object? input, IFormatProvider? provider, out T value)Type parameters
T- The target type to convert to.
Parameters
inputobject?- The value to convert.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valueT- When this method returns, contains the converted value if the conversion succeeded, or the default value if the conversion failed.
Returns
bool true if the conversion succeeded; otherwise, false.
TryChangeType<T>(object?, T)
ConvertUtilities.csAttempts to convert an input value to the specified type using the default converter and invariant culture.