As Method
Definition
- Namespace
- Cuemon.Extensions
- Assemblies
- Cuemon.Extensions.Core.dll
As<T>(object, T, Action<ObjectFormattingOptions>)
Attempts to converts the specified value to a given type. If the conversion is not possible the result is set to fallbackResult.
public static T As<T>(this object value, T fallbackResult = default, Action<ObjectFormattingOptions> setup = null)
Parameters
valueobjectThe object to convert the underlying type.
fallbackResultTThe value to return when a conversion is not possible. Default is
defaultofT.setupAction<ObjectFormattingOptions>The ObjectFormattingOptions which may be configured.
Returns
- T
The
valueconverted to the specifiedT.
Type Parameters
TThe type of the object to return.
As<T, TResult>(T, Func<T, TResult>)
Converts the specified value to a value of TResult.
public static TResult As<T, TResult>(this T value, Func<T, TResult> converter)
Parameters
valueTThe value to convert.
converterFunc<T, TResult>The function delegate that will perform the conversion.
Returns
- TResult
The
valueconverted to the specifiedT.
Type Parameters
TThe type of the value to convert.
TResultThe type of the value to return.
Exceptions
- ArgumentNullException
valuecannot be null.
As(object, Type, Action<ObjectFormattingOptions>)
Attempts to converts the specified value to the given targetType.
public static object As(this object value, Type targetType, Action<ObjectFormattingOptions> setup = null)
Parameters
valueobjectThe object to convert the underlying type.
targetTypeTypeThe type of the object to return.
setupAction<ObjectFormattingOptions>The ObjectFormattingOptions which may be configured.
Returns
Remarks
What differs from the ChangeType(object, Type) is, that this converter supports generics and enums. Fallback uses TypeDescriptor and checks if the underlying IFormatProvider of FormatProvider is a CultureInfo, then this will be used in the conversion together with DescriptorContext.
Exceptions
- ArgumentNullException
valuecannot be null - or -targetTypecannot be null.- AggregateException
valuecould not be converted.
- See Also