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 : IConverter

Example

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

NameDescription
DefaultConverter()

DefaultConverter()

DefaultConverter.cs
public DefaultConverter()

No documentation is available.

Properties

NameDescription
ByteArrayToStringFormatGets or sets the format to use when converting byte arrays to strings.

ByteArrayToStringFormat

DefaultConverter.cs

Gets or sets the format to use when converting byte arrays to strings.

public ByteArrayToStringFormat ByteArrayToStringFormat { get; set; }

Methods

NameDescription
TryChangeType(object?, Type, IFormatProvider?, object?)Attempts to convert an input value to the specified type.
TryConvert(byte[], IFormatProvider?, string?)Converts a byte array to a string using the configured DefaultConverter.ByteArrayToStringFormat.
TryConvert(DateTime, IFormatProvider?, byte[]?)Converts a DateTime to its binary representation as a byte array.
TryConvert(decimal, IFormatProvider?, byte[]?)Converts a decimal to its binary representation as a byte array.
TryConvert(Guid, IFormatProvider?, byte[]?)Converts a Guid to its binary representation as a byte array.
TryConvert(int, IFormatProvider?, CultureInfo?)Converts a locale identifier (LCID) to a CultureInfo instance.
TryConvert(object?, IFormatProvider?, bool)
TryConvert(object?, IFormatProvider?, byte)
TryConvert(object?, IFormatProvider?, byte[]?)Converts various types (Guid, DateTimeOffset, TimeSpan) to byte arrays.
TryConvert(object?, IFormatProvider?, char)
TryConvert(object?, IFormatProvider?, DateTime)
TryConvert(object?, IFormatProvider?, DateTimeOffset)
TryConvert(object?, IFormatProvider?, decimal)
TryConvert(object?, IFormatProvider?, double)
TryConvert(object?, IFormatProvider?, CultureInfo?)Converts a string or integer to a CultureInfo instance.
TryConvert(object?, IFormatProvider?, Guid)
TryConvert(object?, IFormatProvider?, short)
TryConvert(object?, IFormatProvider?, int)
TryConvert(object?, IFormatProvider?, long)
TryConvert(object?, IFormatProvider?, sbyte)
TryConvert(object?, IFormatProvider?, float)
TryConvert(object?, IFormatProvider?, TimeSpan)
TryConvert(object?, IFormatProvider?, ushort)
TryConvert(object?, IFormatProvider?, uint)
TryConvert(object?, IFormatProvider?, ulong)
TryConvert(object?, Type, IFormatProvider?, object?)
TryConvert(string?, IFormatProvider?, byte[]?)Converts a string to a byte array, supporting Base64 and hexadecimal formats.
TryConvert(TimeSpan, IFormatProvider?, byte[]?)Converts a TimeSpan to its binary representation as a byte array.
TryConvertEnum(object?, Type, IFormatProvider?, object?)Converts a string to an enum value.
TryConvertUsingImplicitConverter(object?, Type, IFormatProvider?, object?)Attempts to convert a value using implicit conversion operators defined on the types.

TryChangeType(object?, Type, IFormatProvider?, object?)

DefaultConverter.cs

Attempts to convert an input value to the specified type.

public virtual bool TryChangeType(object? input, Type conversionType, IFormatProvider? provider, out object? value)

Parameters

input object?
The value to convert.
conversionType Type
The type to convert to.
provider IFormatProvider?
An object that provides culture-specific formatting information.
value object?
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.cs

Converts a byte array to a string using the configured DefaultConverter.ByteArrayToStringFormat.

protected virtual bool TryConvert(byte[] input, IFormatProvider? provider, out string? value)
Parameters
input byte[]
The byte array to convert.
provider IFormatProvider?
An object that provides culture-specific formatting information.
value string?
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.cs

Converts a DateTime to its binary representation as a byte array.

protected virtual bool TryConvert(DateTime input, IFormatProvider? provider, out byte[]? value)
Parameters
input DateTime
The DateTime to convert.
provider IFormatProvider?
An object that provides culture-specific formatting information.
value byte[]?
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.cs

Converts a decimal to its binary representation as a byte array.

protected virtual bool TryConvert(decimal input, IFormatProvider? provider, out byte[]? value)
Parameters
input decimal
The decimal to convert.
provider IFormatProvider?
An object that provides culture-specific formatting information.
value byte[]?
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.cs

Converts a Guid to its binary representation as a byte array.

protected virtual bool TryConvert(Guid input, IFormatProvider? provider, out byte[]? value)
Parameters
input Guid
The Guid to convert.
provider IFormatProvider?
An object that provides culture-specific formatting information.
value byte[]?
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.cs

Converts a locale identifier (LCID) to a CultureInfo instance.

protected virtual bool TryConvert(int lcid, IFormatProvider? provider, out CultureInfo? value)
Parameters
lcid int
The LCID to convert.
provider IFormatProvider?
An object that provides culture-specific formatting information.
value CultureInfo?
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.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out bool value)
Parameters
input object?
provider IFormatProvider?
value bool
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, byte)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out byte value)
Parameters
input object?
provider IFormatProvider?
value byte
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, byte[]?)

DefaultConverter.cs

Converts various types (Guid, DateTimeOffset, TimeSpan) to byte arrays.

protected virtual bool TryConvert(object? input, IFormatProvider? provider, out byte[]? value)
Parameters
input object?
The value to convert.
provider IFormatProvider?
An object that provides culture-specific formatting information.
value byte[]?
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.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out char value)
Parameters
input object?
provider IFormatProvider?
value char
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, DateTime)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out DateTime value)
Parameters
input object?
provider IFormatProvider?
value DateTime
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, DateTimeOffset)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out DateTimeOffset value)
Parameters
input object?
provider IFormatProvider?
value DateTimeOffset
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, decimal)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out decimal value)
Parameters
input object?
provider IFormatProvider?
value decimal
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, double)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out double value)
Parameters
input object?
provider IFormatProvider?
value double
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, CultureInfo?)

DefaultConverter.cs

Converts a string or integer to a CultureInfo instance.

protected virtual bool TryConvert(object? input, IFormatProvider? provider, out CultureInfo? value)
Parameters
input object?
The value to convert (culture name or LCID).
provider IFormatProvider?
An object that provides culture-specific formatting information.
value CultureInfo?
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.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out Guid value)
Parameters
input object?
provider IFormatProvider?
value Guid
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, short)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out short value)
Parameters
input object?
provider IFormatProvider?
value short
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, int)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out int value)
Parameters
input object?
provider IFormatProvider?
value int
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, long)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out long value)
Parameters
input object?
provider IFormatProvider?
value long
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, sbyte)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out sbyte value)
Parameters
input object?
provider IFormatProvider?
value sbyte
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, float)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out float value)
Parameters
input object?
provider IFormatProvider?
value float
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, TimeSpan)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out TimeSpan value)
Parameters
input object?
provider IFormatProvider?
value TimeSpan
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, ushort)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out ushort value)
Parameters
input object?
provider IFormatProvider?
value ushort
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, uint)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out uint value)
Parameters
input object?
provider IFormatProvider?
value uint
Returns

bool

No documentation is available.

TryConvert(object?, IFormatProvider?, ulong)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, IFormatProvider? provider, out ulong value)
Parameters
input object?
provider IFormatProvider?
value ulong
Returns

bool

No documentation is available.

TryConvert(object?, Type, IFormatProvider?, object?)

DefaultConverter.cs
protected virtual bool TryConvert(object? input, Type conversionType, IFormatProvider? provider, out object? value)
Parameters
input object?
conversionType Type
provider IFormatProvider?
value object?
Returns

bool

No documentation is available.

TryConvert(string?, IFormatProvider?, byte[]?)

DefaultConverter.cs

Converts a string to a byte array, supporting Base64 and hexadecimal formats.

protected virtual bool TryConvert(string? text, IFormatProvider? provider, out byte[]? value)
Parameters
text string?
The string to convert.
provider IFormatProvider?
An object that provides culture-specific formatting information.
value byte[]?
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.cs

Converts a TimeSpan to its binary representation as a byte array.

protected virtual bool TryConvert(TimeSpan input, IFormatProvider? provider, out byte[]? value)
Parameters
input TimeSpan
The TimeSpan to convert.
provider IFormatProvider?
An object that provides culture-specific formatting information.
value byte[]?
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.cs

Converts a string to an enum value.

protected virtual bool TryConvertEnum(object? input, Type conversionType, IFormatProvider? provider, out object? value)

Parameters

input object?
The value to convert.
conversionType Type
The enum type to convert to.
provider IFormatProvider?
An object that provides culture-specific formatting information.
value object?
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.cs

Attempts 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

input object?
The value to convert.
conversionType Type
The type to convert to.
provider IFormatProvider?
An object that provides culture-specific formatting information.
value object?
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.