Table of Contents

As Method

Definition

Namespace
Cuemon.Extensions
Assemblies
Cuemon.Extensions.Core.dll
Source
src/Cuemon.Extensions.Core/ObjectExtensions.cs

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

value object

The object to convert the underlying type.

fallbackResult T

The value to return when a conversion is not possible. Default is default of T.

setup Action<ObjectFormattingOptions>

The ObjectFormattingOptions which may be configured.

Returns

T

The value converted to the specified T.

Type Parameters

T

The 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

value T

The value to convert.

converter Func<T, TResult>

The function delegate that will perform the conversion.

Returns

TResult

The value converted to the specified T.

Type Parameters

T

The type of the value to convert.

TResult

The type of the value to return.

Exceptions

ArgumentNullException

value cannot 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

value object

The object to convert the underlying type.

targetType Type

The type of the object to return.

setup Action<ObjectFormattingOptions>

The ObjectFormattingOptions which may be configured.

Returns

object

An Object of type targetType equivalent to value.

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

value cannot be null - or - targetType cannot be null.

AggregateException

value could not be converted.

See Also