Formatter
Provides formatting and parsing utilities for converting between different data types with support for culture-specific formatting and null value handling.
Definition
Namespace:Telerik.WinControls
Assembly:Telerik.WinControls.dll
Syntax:
public class Formatter
Inheritance: objectFormatter
Constructors
Initializes a new instance of the Formatter class.
public Formatter()
This constructor creates a new Formatter instance. However, most operations are provided as static methods, so instance creation is typically not necessary.
Methods
Formats the specified value to the target type using comprehensive formatting rules and type conversion.
public static object FormatObject(object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, string formatString, IFormatProvider formatInfo, object formattedNullValue, object dataSourceNullValue)
The value to format.
targetTypeTypeThe type to format the value to.
sourceConverterTypeConverterOptional custom TypeConverter for the source type.
targetConverterTypeConverterOptional custom TypeConverter for the target type.
formatStringstringOptional format string for IFormattable objects.
formatInfoIFormatProviderOptional format provider for culture-specific formatting.
formattedNullValueobjectThe value to return when the source value represents null.
dataSourceNullValueobjectThe value that represents null in the data source.
Returns:The formatted value as the target type.
Exceptions:Thrown when the formatting operation cannot be completed.
Provides comprehensive formatting including null value handling, custom TypeConverter support, format string application for IFormattable objects, and special handling for CheckState conversions.
Gets the default null value representation for the specified type in data source contexts.
public static object GetDefaultDataSourceNullValue(Type type)
The type to get the default null value for.
Returns:null for reference types; Value for value types.
This method determines the appropriate null value representation based on the type.
Reference types use null as their null representation, while value types
use Value since they cannot be assigned null directly.
This is commonly used in data binding scenarios where the data source needs to represent null values in a type-appropriate manner.
Attempts to invoke a Parse method on the target type to convert a string value to the target type.
public static object InvokeStringParseMethod(object value, Type targetType, IFormatProvider formatInfo)
The string value to parse.
targetTypeTypeThe type to parse the string into.
formatInfoIFormatProviderThe format provider for culture-specific parsing.
Returns:The parsed value if a suitable Parse method was found and executed successfully; otherwise, a sentinel value indicating that no Parse method was found.
Exceptions:Thrown when the Parse method execution fails.
Uses reflection to find and invoke Parse methods, searching for Parse(string, NumberStyles, IFormatProvider), Parse(string, IFormatProvider), and Parse(string) in that order of preference.
Determines whether the specified value represents null data based on the data source null value.
public static bool IsNullData(object value, object dataSourceNullValue)
The value to check for null representation.
dataSourceNullValueobjectThe value that represents null in the data source context.
Returns:true if the value represents null data; otherwise, false.
This method provides flexible null detection that goes beyond simple null checking. It considers both standard null representations (null and DBNull.Value) and custom null value representations defined by the data source.
This is particularly useful in data binding scenarios where different data sources may use different conventions for representing null or missing values.
Gets the appropriate null data representation for the specified type in data source contexts.
public static object NullData(Type type, object dataSourceNullValue)
The type to get the null representation for.
dataSourceNullValueobjectThe data source's null value representation.
Returns:null for nullable types when dataSourceNullValue is null or DBNull.Value;
otherwise, the dataSourceNullValue for non-nullable types.
This method determines the correct null representation based on whether the type is nullable (Nullable<T>) and the data source's null value convention.
For nullable types, null is preferred when the data source uses null or DBNull.Value. For non-nullable types, the data source's null value representation is used.
Parses the specified value from the source type to the target type using comprehensive parsing rules.
public static object ParseObject(object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, object formattedNullValue, object dataSourceNullValue)
The value to parse.
targetTypeTypeThe type to parse the value to.
sourceTypeTypeThe type of the source value.
targetConverterTypeConverterOptional custom TypeConverter for the target type.
sourceConverterTypeConverterOptional custom TypeConverter for the source type.
formatInfoIFormatProviderOptional format provider for culture-specific parsing.
formattedNullValueobjectThe formatted representation of null values.
dataSourceNullValueobjectThe value that represents null in the data source.
Returns:The parsed value as the target type, or the appropriate null representation.
Exceptions:Thrown when the parsing operation cannot be completed.
Provides comprehensive parsing capabilities including formatted null value detection, custom TypeConverter support, automatic Parse method invocation, and CheckState conversions.