TypeConverter

The TypeConverter class allows you to convert arbitrary values into objects specified by a code type and to get the code type of an object.

Description

The TypeConverter class allows you to convert arbitrary values into objects specified by a code type and to get the code type of an object. The code types are defined in the TypeCode class.

Static methods

asString

Converts a TypeCode into its string name.

static String asString(TypeCode type)

  • type: TypeCode - TypeCode to convert into a string.
  • returns: String - name of the TypeCode passed as a string value.

toNullableType

Converts a value into an object of the type specified by a TypeCode or returns null when the conversion is not possible.

static T? toNullableType<T>(TypeCode? type, value)

  • type: TypeCode - type code for the data type into which ‘value’ is to be converted.
  • value: dynamic - value to convert.
  • returns: T? - object value of type corresponding to TypeCode, or null when the conversion is not supported.

toType

Converts a value into an object of the type specified by Type Code or returns an object of default type when the conversion is not possible.

static T toType<T>(TypeCode type, value)

  • type: TypeCode - type to which the value is to be converted into.
  • value: dynamic - value to convert.
  • returns: T - object value of type corresponding to TypeCode, or object of default type when the conversion is not supported.

toTypeCode

Gets a TypeCode for a specific value.

static TypeCode toTypeCode(value)

  • value: dynamic - value whose TypeCode is to be resolved.
  • returns: TypeCode - TypeCode that corresponds to the passed object’s type.

toTypeWithDefault

Converts a value into an object of the type specified by Type Code or returns a default value when the conversion is not possible.

static T toTypeWithDefault<T>(TypeCode type, value, T defaultValue)

  • type: TypeCode - TypeCode for the data type into which ‘value’ is to be converted.
  • value: dynamic - value to convert.
  • defaultValue: T - default value to return if conversion is not possible (returns null).
  • returns: T - object value of type corresponding to TypeCode, or default value when the conversion is not supported.

Examples

var value1 = TypeConverter.toType(TypeCode.Integer, '123.456'); // Result: 123
var value2 = TypeConverter.toType(TypeCode.DateTime, 123); // Result: DateTime(123)
var value3 = TypeConverter.toType(TypeCode.Boolean, 'F'); // Result: false

See also