Class ObjectExtensions
- Namespace
- Cuemon.Extensions
- Assembly
- Cuemon.Extensions.Core.dll
Extension methods for the System.Object class.
public static class ObjectExtensions
- Inheritance
-
ObjectObjectExtensions
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
value
TThe value to convert.
converter
Func<T, T>The function delegate that will convert the specified
value
.
Returns
- T
The
value
in its original or converted form.
Type Parameters
T
The 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
value
TThe value to adjust.
modifier
Action<T>The delegate that will adjust the specified
value
.
Returns
- T
The
value
in its original or adjusted form.
Type Parameters
T
The 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
value
ObjectThe object to convert the underlying type.
targetType
TypeThe type of the object to return.
setup
Action<ObjectFormattingOptions>The ObjectFormattingOptions which may be configured.
Returns
- Object
An System.Object of type
targetType
equivalent tovalue
.
Remarks
What differs from the System.Convert.ChangeType(System.Object,System.Type) is, that this converter supports generics and enums. Fallback uses System.ComponentModel.TypeDescriptor and checks if the underlying System.IFormatProvider of
Exceptions
- System.ArgumentNullException
value
cannot be null - or -targetType
cannot be null.- System.AggregateException
value
could not be converted.
- See Also
-
System.Convert.ChangeType(System.Object,System.Type)System.ComponentModel.TypeDescriptor.GetConverter(System.Type)
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 = null, Action<ObjectFormattingOptions> setup = null)
Parameters
value
ObjectThe object to convert the underlying type.
fallbackResult
TThe value to return when a conversion is not possible. Default is
default
ofT
.setup
Action<ObjectFormattingOptions>The ObjectFormattingOptions which may be configured.
Returns
- T
The
value
converted to the specifiedT
.
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
TThe value to convert.
converter
Func<T, TResult>The function delegate that will perform the conversion.
Returns
- TResult
The
value
converted to the specifiedT
.
Type Parameters
T
The type of the value to convert.
TResult
The type of the value to return.
Exceptions
- System.ArgumentNullException
value
cannot 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
convertibles
IEnumerable<T>A sequence of objects implementing the System.IConvertible interface.
Returns
- Int32
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
convertibles
IEnumerable<T>A sequence of objects implementing the System.IConvertible interface.
Returns
- Int64
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 System.ValueType.
public static bool IsNullable<T>(this T _)
Parameters
_
TThe source type to check for nullable System.ValueType.
Returns
- Boolean
true
if the specified source is nullable; otherwise,false
.
Type Parameters
T
The 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
source
IEnumerable<T>A sequence of elements to be converted.
setup
Action<DelimitedStringOptions<T>>The DelimitedStringOptions<T> which may be configured.
Returns
- String
A System.String of delimited values.
Type Parameters
T
The type of the elements of the sequence to convert.
Exceptions
- System.ArgumentNullException
source
cannot 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
instance
TThe instance to wrap and extend.
extender
Action<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
T
The 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
instance
TThe instance to wrap and extend.
memberReference
MemberInfoThe optional member reference to assign MemberReference.
extender
Action<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
T
The type of the object to extend.