DefaultConverter Class
Provides a powerful type converter that extends the capabilities of System.Convert with support for culture-specific formatting, byte arrays, enums, nullable types, and custom TypeConverters.
public class DefaultConverter : IConverterExample
var converter = new DefaultConverter();
// Convert string to int
Assert.True(converter.TryChangeType("42", typeof(int), CultureInfo.InvariantCulture, out var result));
Assert.Equal(42, result);
// Convert byte array to hex string
converter.ByteArrayToStringFormat = ByteArrayToStringFormat.Base16Prefixed;
Assert.True(converter.TryChangeType(new byte[] { 1, 2, 3, 4 }, typeof(string), CultureInfo.InvariantCulture, out var hex));
Assert.Equal("0x01020304", hex);
// Convert hex string to byte array
Assert.True(converter.TryChangeType("0x01020304", typeof(byte[]), CultureInfo.InvariantCulture, out var bytes));
Assert.Equal(new byte[] { 1, 2, 3, 4 }, (byte[]?)bytes);
// Parse enum with flags
Assert.True(converter.TryChangeType("ReadOnly, Hidden", typeof(FileAttributes), CultureInfo.InvariantCulture, out var enumValue));
Assert.Equal(FileAttributes.ReadOnly | FileAttributes.Hidden, enumValue);- Implements
Constructors
| Name | Description |
|---|---|
| DefaultConverter() |
DefaultConverter()
DefaultConverter.cspublic DefaultConverter()No documentation is available.
Properties
| Name | Description |
|---|---|
| ByteArrayToStringFormat | Gets or sets the format to use when converting byte arrays to strings. |
ByteArrayToStringFormat
DefaultConverter.csGets or sets the format to use when converting byte arrays to strings.
public ByteArrayToStringFormat ByteArrayToStringFormat { get; set; }Property value
Methods
TryChangeType(object?, Type, IFormatProvider?, object?)
DefaultConverter.csAttempts to convert an input value to the specified type.
public virtual 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.
TryConvert overloads
TryConvert(byte[], IFormatProvider?, string?)
DefaultConverter.csConverts a byte array to a string using the configured DefaultConverter.ByteArrayToStringFormat.
protected virtual bool TryConvert(byte[] input, IFormatProvider? provider, out string? value)Parameters
inputbyte[]- The byte array to convert.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valuestring?- When this method returns, contains the string representation of the byte array.
Returns
bool true if the conversion succeeded; otherwise, false.
TryConvert(DateTime, IFormatProvider?, byte[]?)
DefaultConverter.csConverts a DateTime to its binary representation as a byte array.
protected virtual bool TryConvert(DateTime input, IFormatProvider? provider, out byte[]? value)Parameters
inputDateTime- The DateTime to convert.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valuebyte[]?- When this method returns, contains the byte array representation of the DateTime.
Returns
bool true if the conversion succeeded; otherwise, false.
TryConvert(decimal, IFormatProvider?, byte[]?)
DefaultConverter.csConverts a decimal to its binary representation as a byte array.
protected virtual bool TryConvert(decimal input, IFormatProvider? provider, out byte[]? value)Parameters
inputdecimal- The decimal to convert.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valuebyte[]?- When this method returns, contains the byte array representation of the decimal.
Returns
bool true if the conversion succeeded; otherwise, false.
TryConvert(Guid, IFormatProvider?, byte[]?)
DefaultConverter.csConverts a Guid to its binary representation as a byte array.
protected virtual bool TryConvert(Guid input, IFormatProvider? provider, out byte[]? value)Parameters
inputGuid- The Guid to convert.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valuebyte[]?- When this method returns, contains the byte array representation of the Guid.
Returns
bool true if the conversion succeeded; otherwise, false.
TryConvert(int, IFormatProvider?, CultureInfo?)
DefaultConverter.csConverts a locale identifier (LCID) to a CultureInfo instance.
protected virtual bool TryConvert(int lcid, IFormatProvider? provider, out CultureInfo? value)Parameters
lcidint- The LCID to convert.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valueCultureInfo?- When this method returns, contains the CultureInfo instance if conversion succeeded.
Returns
bool true if the conversion succeeded; otherwise, false.
TryConvert(object?, IFormatProvider?, bool)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out bool value)No documentation is available.
TryConvert(object?, IFormatProvider?, byte)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out byte value)No documentation is available.
TryConvert(object?, IFormatProvider?, byte[]?)
DefaultConverter.csConverts various types (Guid, DateTimeOffset, TimeSpan) to byte arrays.
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out byte[]? value)Parameters
inputobject?- The value to convert.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valuebyte[]?- When this method returns, contains the byte array representation if conversion succeeded.
Returns
bool true if the conversion succeeded; otherwise, false.
TryConvert(object?, IFormatProvider?, char)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out char value)No documentation is available.
TryConvert(object?, IFormatProvider?, DateTime)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out DateTime value)No documentation is available.
TryConvert(object?, IFormatProvider?, DateTimeOffset)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out DateTimeOffset value)No documentation is available.
TryConvert(object?, IFormatProvider?, decimal)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out decimal value)No documentation is available.
TryConvert(object?, IFormatProvider?, double)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out double value)No documentation is available.
TryConvert(object?, IFormatProvider?, CultureInfo?)
DefaultConverter.csConverts a string or integer to a CultureInfo instance.
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out CultureInfo? value)Parameters
inputobject?- The value to convert (culture name or LCID).
providerIFormatProvider?- An object that provides culture-specific formatting information.
valueCultureInfo?- When this method returns, contains the CultureInfo instance if conversion succeeded.
Returns
bool true if the conversion succeeded; otherwise, false.
TryConvert(object?, IFormatProvider?, Guid)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out Guid value)No documentation is available.
TryConvert(object?, IFormatProvider?, short)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out short value)No documentation is available.
TryConvert(object?, IFormatProvider?, int)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out int value)No documentation is available.
TryConvert(object?, IFormatProvider?, long)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out long value)No documentation is available.
TryConvert(object?, IFormatProvider?, sbyte)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out sbyte value)No documentation is available.
TryConvert(object?, IFormatProvider?, float)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out float value)No documentation is available.
TryConvert(object?, IFormatProvider?, TimeSpan)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out TimeSpan value)No documentation is available.
TryConvert(object?, IFormatProvider?, ushort)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out ushort value)No documentation is available.
TryConvert(object?, IFormatProvider?, uint)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out uint value)No documentation is available.
TryConvert(object?, IFormatProvider?, ulong)
DefaultConverter.csprotected virtual bool TryConvert(object? input, IFormatProvider? provider, out ulong value)No documentation is available.
TryConvert(object?, Type, IFormatProvider?, object?)
DefaultConverter.csprotected virtual bool TryConvert(object? input, Type conversionType, IFormatProvider? provider, out object? value)No documentation is available.
TryConvert(string?, IFormatProvider?, byte[]?)
DefaultConverter.csConverts a string to a byte array, supporting Base64 and hexadecimal formats.
protected virtual bool TryConvert(string? text, IFormatProvider? provider, out byte[]? value)Parameters
textstring?- The string to convert.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valuebyte[]?- When this method returns, contains the byte array if conversion succeeded.
Returns
bool true if the conversion succeeded; otherwise, false.
TryConvert(TimeSpan, IFormatProvider?, byte[]?)
DefaultConverter.csConverts a TimeSpan to its binary representation as a byte array.
protected virtual bool TryConvert(TimeSpan input, IFormatProvider? provider, out byte[]? value)Parameters
inputTimeSpan- The TimeSpan to convert.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valuebyte[]?- When this method returns, contains the byte array representation of the TimeSpan.
Returns
bool true if the conversion succeeded; otherwise, false.
TryConvertEnum(object?, Type, IFormatProvider?, object?)
DefaultConverter.csConverts a string to an enum value.
protected virtual bool TryConvertEnum(object? input, Type conversionType, IFormatProvider? provider, out object? value)Parameters
inputobject?- The value to convert.
conversionTypeType- The enum type to convert to.
providerIFormatProvider?- An object that provides culture-specific formatting information.
valueobject?- When this method returns, contains the enum value if conversion succeeded.
Returns
bool true if the conversion succeeded; otherwise, false.
TryConvertUsingImplicitConverter(object?, Type, IFormatProvider?, object?)
DefaultConverter.csAttempts to convert a value using implicit conversion operators defined on the types.
protected virtual bool TryConvertUsingImplicitConverter(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 an implicit conversion operator was found and succeeded.
Returns
bool true if an implicit conversion operator was found and succeeded; otherwise, false.