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
valueobjectAn object that supplies the value to convert.
conversionTypeTypeThe type to which to convert the value.
cultureCultureInfoA CultureInfo. If null is passed, the current culture is assumed.
Returns
- object
The converted value.
Remarks
- If
valueis null andconversionTypeis a reference type or Nullable<T>, then null is returned. - If
valueis a string andconversionTypeis a Nullable<T>:- If
valueis an empty string, then null is returned. - Otherwise,
conversionType's underlying Nullable<T> type is used in place ofconversionTypefor the target type for the rest of the steps below.
- If
- If
valueis already the same type asconversionType, then the originalvalueis returned. - If
valueis assignable to aconversionType, then the originalvalueis returned. - If
conversionTypeis Type, then ToType(object) is used to perform the conversion. - If the type converter for
value's type can convert the value toconversionType, then it is used to perform the conversion. - If the type converter for
conversionTypecan convert the value fromvalue's type, then it is used to perform the conversion. - If
valueimplements IConvertible, then it is used to attempt the conversion.
Exceptions
- ArgumentNullException
conversionTypeis null.- InvalidOperationException
Cannot convert null to
conversionType.- InvalidOperationException
Cannot convert from
value's type toconversionType.- FormatException
valueis not in a format recognized byconversionType.- OverflowException
valuerepresents a number that is out of the range ofconversionType.
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
valueobjectAn object that supplies the value to convert.
conversionTypeTypeThe 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
conversionTypeis null.- InvalidOperationException
Cannot convert null to
conversionType.- InvalidOperationException
Cannot convert from
value's type toconversionType.- FormatException
valueis not in a format recognized byconversionType.- OverflowException
valuerepresents a number that is out of the range ofconversionType.
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
valueobjectAn 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
TThe 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
valueobjectAn object that supplies the value to convert.
cultureCultureInfoA 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
TThe 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 toT.- FormatException
valueis not in a format recognized byT.- OverflowException
valuerepresents a number that is out of the range ofT.
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
valueobjectAn object that supplies the value to convert.
Returns
- string
The string representation of
value, or Empty ifvalueis 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
Returns
Remarks
The following are supported for value:
- A
valuethat 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
valueis 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
valuewhen it is a string.