Class ObjectExtensions
- Namespace
- Cuemon.Extensions
- Assembly
- Cuemon.Extensions.Core.dll
Extension methods for the Object class.
public static class ObjectExtensions
- Inheritance
-
ObjectExtensions
Methods
Adjust<T>(T, Func<T, T>)
Adjust the specified value with the function delegate converter.
public static T Adjust<T>(this T value, Func<T, T> converter)
Parameters
valueTThe value to convert.
converterFunc<T, T>The function delegate that will convert the specified
value.
Returns
- T
The
valuein its original or converted form.
Type Parameters
TThe type of the value to convert.
Remarks
This is thought to be a more severe change than the one provided by Alter<T>(T, Action<T>) (e.g., potentially convert the entire value to a new instance).
Alter<T>(T, Action<T>)
Adjust the specified value with the modifier delegate.
public static T Alter<T>(this T value, Action<T> modifier)
Parameters
valueTThe value to adjust.
modifierAction<T>The delegate that will adjust the specified
value.
Returns
- T
The
valuein its original or adjusted form.
Type Parameters
TThe type of the value to adjust.
Remarks
This is thought to be a more relaxed change than the one provided by Adjust<T>(T, Func<T, T>) (e.g., applying changes only to the current value).
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
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.
GetHashCode32<T>(IEnumerable<T>)
Computes a suitable hash code from the specified sequence of convertibles.
public static int GetHashCode32<T>(this IEnumerable<T> convertibles) where T : IConvertible
Parameters
convertiblesIEnumerable<T>A sequence of objects implementing the IConvertible interface.
Returns
- int
A 32-bit signed integer that is the hash code of
convertibles.
Type Parameters
T
GetHashCode64<T>(IEnumerable<T>)
Computes a suitable hash code from the specified sequence of convertibles.
public static long GetHashCode64<T>(this IEnumerable<T> convertibles) where T : IConvertible
Parameters
convertiblesIEnumerable<T>A sequence of objects implementing the IConvertible interface.
Returns
- long
A 64-bit signed integer that is the hash code of
convertibles.
Type Parameters
T
IsNullable<T>(T)
Determines whether the specified source is a nullable ValueType.
public static bool IsNullable<T>(this T _)
Parameters
_TThe source type to check for nullable ValueType.
Returns
- bool
trueif the specified source is nullable; otherwise,false.
Type Parameters
TThe type of the
_ofT.
ToDelimitedString<T>(IEnumerable<T>, Action<DelimitedStringOptions<T>>)
Converts the specified source to a string of delimited values.
public static string ToDelimitedString<T>(this IEnumerable<T> source, Action<DelimitedStringOptions<T>> setup = null)
Parameters
sourceIEnumerable<T>A sequence of elements to be converted.
setupAction<DelimitedStringOptions<T>>The DelimitedStringOptions<T> which may be configured.
Returns
Type Parameters
TThe type of the elements of the sequence to convert.
Exceptions
- ArgumentNullException
sourcecannot be null.
UseWrapper<T>(T, Action<IDictionary<string, object>>)
Wrap and extend an existing object of T with additional data.
public static IWrapper<T> UseWrapper<T>(this T instance, Action<IDictionary<string, object>> extender = null)
Parameters
instanceTThe instance to wrap and extend.
extenderAction<IDictionary<string, object>>The delegate that provides an easy way of supplying additional data to an object.
Returns
- IWrapper<T>
An implementation of IWrapper<T> encapsulating the specified
instance.
Type Parameters
TThe type of the object to extend.
UseWrapper<T>(T, MemberInfo, Action<IDictionary<string, object>>)
Wrap and extend an existing object of T with additional data.
public static IWrapper<T> UseWrapper<T>(this T instance, MemberInfo memberReference, Action<IDictionary<string, object>> extender = null)
Parameters
instanceTThe instance to wrap and extend.
memberReferenceMemberInfoThe optional member reference to assign MemberReference.
extenderAction<IDictionary<string, object>>The delegate that provides an easy way of supplying additional data to an object.
Returns
- IWrapper<T>
An implementation of IWrapper<T> encapsulating the specified
instance.
Type Parameters
TThe type of the object to extend.