Table of Contents

Class SmartConvert

Namespace
Acuit.Pinpoint.Common
Assembly
Acuit.Pinpoint.Common.dll

Converts values to other types.

public static class SmartConvert
Inheritance
SmartConvert
Inherited Members

Methods

ChangeType(object?, Type, CultureInfo?)

Returns an object of the specified type and whose value is equivalent to the specified object, using any conversion method available.

public static object? ChangeType(object? value, Type conversionType, CultureInfo? culture)

Parameters

value object

An object that supplies the value to convert.

conversionType Type

The type to which to convert the value.

culture CultureInfo

A CultureInfo. If null is passed, the current culture is assumed.

Returns

object

The converted value.

Remarks

  1. If value is null and conversionType is a reference type or Nullable<T>, then null is returned.
  2. If value is a string and conversionType is a Nullable<T>:
    1. If value is an empty string, then null is returned.
    2. Otherwise, conversionType's underlying Nullable<T> type is used in place of conversionType for the target type for the rest of the steps below.
  3. If value is already the same type as conversionType, then the original value is returned.
  4. If value is assignable to a conversionType, then the original value is returned.
  5. If conversionType is Type, then ToType(object) is used to perform the conversion.
  6. If the type converter for value's type can convert the value to conversionType, then it is used to perform the conversion.
  7. If the type converter for conversionType can convert the value from value's type, then it is used to perform the conversion.
  8. If value implements IConvertible, then it is used to attempt the conversion.

Exceptions

ArgumentNullException

conversionType is null.

InvalidOperationException

Cannot convert null to conversionType.

InvalidOperationException

Cannot convert from value's type to conversionType.

FormatException

value is not in a format recognized by conversionType.

OverflowException

value represents a number that is out of the range of conversionType.

ChangeTypeInvariant(object?, Type)

Returns an object of the specified type and whose value is equivalent to the specified object, using any conversion method available, using the invariant culture.

public static object? ChangeTypeInvariant(object? value, Type conversionType)

Parameters

value object

An object that supplies the value to convert.

conversionType Type

The type to which to convert the value.

Returns

object

The converted value.

Remarks

See the remarks for ChangeType(object?, Type, CultureInfo?) for more information.

Exceptions

ArgumentNullException

conversionType is null.

InvalidOperationException

Cannot convert null to conversionType.

InvalidOperationException

Cannot convert from value's type to conversionType.

FormatException

value is not in a format recognized by conversionType.

OverflowException

value represents a number that is out of the range of conversionType.

ChangeTypeInvariant<T>(object?)

Returns an object of the specified type and whose value is equivalent to the specified object, using any conversion method available, using the invariant culture.

public static T ChangeTypeInvariant<T>(object? value)

Parameters

value object

An object that supplies the value to convert.

Returns

T

The converted value. This may be null for reference types, even though a non-nullable reference type was specified for T.

Type Parameters

T

The type to which to convert the value.

Remarks

See the remarks for ChangeType(object?, Type, CultureInfo?) for more information.

ChangeType<T>(object?, CultureInfo?)

Returns an object of the specified type and whose value is equivalent to the specified object, using any conversion method available.

public static T ChangeType<T>(object? value, CultureInfo? culture)

Parameters

value object

An object that supplies the value to convert.

culture CultureInfo

A CultureInfo. If null is passed, the current culture is assumed.

Returns

T

The converted value. This may be null for reference types, even though a non-nullable reference type was specified for T.

Type Parameters

T

The type to which to convert the value.

Remarks

See the remarks for ChangeType(object?, Type, CultureInfo?) for more information.

Exceptions

InvalidOperationException

Cannot convert null to T.

InvalidOperationException

Cannot convert from value's type to T.

FormatException

value is not in a format recognized by T.

OverflowException

value represents a number that is out of the range of T.

ToStringInvariant(object?)

Converts the value of the specified object to its equivalent string representation using the invariant culture formatting information.

public static string ToStringInvariant(object? value)

Parameters

value object

An object that supplies the value to convert.

Returns

string

The string representation of value, or Empty if value is an object whose value is null or if the object's ToString() implementation was used and it returned null.

ToType(object)

Converts the value of the specified object representing a type to its equivalent Type.

public static Type ToType(object value)

Parameters

value object

The object specifying a type, which must either by a Type or a string.

Returns

Type

The Type that is equivalent to value.

Remarks

The following are supported for value:

  • A value that is already a Type is returned as-is.
  • C# built-in types are supported by name (case-insensitive).
  • Full type names of any type in this assembly or mscorlib (case-insensitive).
  • Type names of any type in mscorlib in the "System" namespace (case-insensitive).
  • Full type names of any type in any loaded assembly or mscorlib (case-sensitive; the first one found is used when the same type name exists in multiple assembies).
  • A string that ends with "[]" is resolved as an array type.
  • A string that ends with "?" is resolved as a nullable type.

Exceptions

ArgumentNullException

value is null.

InvalidOperationException

A type cannot be resolved from an object of the type of value.

InvalidOperationException

A type cannot be resolved from the value of value when it is a string.