Class TypeUtilities
Provides utilities for working with types at runtime.
public static class TypeUtilities
- Inheritance
-
TypeUtilities
- Inherited Members
Methods
AcceptsNull(Type)
Returns a value indicating whether null can be assigned to the specified type.
public static bool AcceptsNull(Type type)
Parameters
type
TypeThe type.
Returns
- bool
True if the type accepts null values; otherwise false.
AcceptsNull<T>()
Returns a value indicating whether null can be assigned to the specified type.
public static bool AcceptsNull<T>()
Returns
- bool
True if the type accepts null values; otherwise false.
Type Parameters
T
The type
CanCast<T>(object?)
Returns a value indicating whether value can be casted to the specified type. If value is null, checks if instances of that type can be null.
public static bool CanCast<T>(object? value)
Parameters
value
objectThe value to check if cast possible
Returns
- bool
True if the cast is possible, otherwise false.
Type Parameters
T
The type to cast to
ConvertImplicitOrDefault(object?, Type)
Convert a value to a type using the implicit conversions allowed by the C# language or return the default for the type if the value could not be converted.
public static object? ConvertImplicitOrDefault(object? value, Type type)
Parameters
Returns
- object
A value of
type
.
ConvertImplicit<T>(object?)
public static T ConvertImplicit<T>(object? value)
Parameters
value
object
Returns
- T
Type Parameters
T
ConvertOrDefault(object?, Type, CultureInfo)
Convert a value to a type by any means possible, returning the default for that type if the value could not be converted.
public static object? ConvertOrDefault(object? value, Type type, CultureInfo culture)
Parameters
value
objectThe value to convert.
type
TypeThe type to convert to.
culture
CultureInfoThe culture to use.
Returns
- object
A value of
type
.
Default(Type)
Gets the default value for the specified type.
public static object? Default(Type type)
Parameters
type
TypeThe type.
Returns
- object
The default value.
IsNumeric(Type)
Determines if a type is numeric. Nullable numeric types are considered numeric.
public static bool IsNumeric(Type type)
Parameters
type
Type
Returns
- bool
True if the type is numeric; otherwise false.
Remarks
Boolean is not considered numeric.
TryConvert(Type, object?, CultureInfo?, out object?)
Try to convert a value to a type by any means possible.
public static bool TryConvert(Type to, object? value, CultureInfo? culture, out object? result)
Parameters
to
TypeThe type to convert to.
value
objectThe value to convert.
culture
CultureInfoThe culture to use.
result
objectIf successful, contains the convert value.
Returns
- bool
True if the cast was successful, otherwise false.
TryConvertImplicit(Type, object?, out object?)
Try to convert a value to a type using the implicit conversions allowed by the C# language.
public static bool TryConvertImplicit(Type to, object? value, out object? result)
Parameters
to
TypeThe type to convert to.
value
objectThe value to convert.
result
objectIf successful, contains the converted value.
Returns
- bool
True if the convert was successful, otherwise false.