Repository navigation
OnixLabs.Core.ObjectExtensions
Extension methods for object instances, including functional pipe helpers, record-style string formatting, and conversions to Optional<T> and Result<T>.
| Namespace | OnixLabs.Core |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → ObjectExtensions
|
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ObjectExtensionsNote This class is decorated with
[EditorBrowsable(EditorBrowsableState.Never)], which keeps the container class itself out of IntelliSense lists while its extension methods remain fully usable on the receiver types. Bring the methods into scope withusing OnixLabs.Core;.
ObjectExtensions provides functional-style pipe helpers (Apply, Let), null-safe string formatting (ToRecordString, ToStringOrNull), null extraction (TryGetNonNull), and conversions to the Optional<T> and Success<T> result types — including their async (Task<T>) counterparts. The async overloads live alongside their synchronous siblings in the same class.
| Method | Returns | Description |
|---|---|---|
Apply<T>(this T, Action<T>) |
T |
Invokes an action with the current reference-type value and returns the value. |
Apply<T>(this T, Func<T, T>) |
T |
Invokes a function with the current value-type value and returns its result. |
Let<TSource, TResult>(this TSource, Func<TSource, TResult>) |
TResult |
Invokes a function with the current value and returns its result. |
ToRecordString(this object?) |
string |
Gets a record-like string representation of the current object. |
ToStringOrNull(this object?) |
string |
Gets a string representation, or the literal "null" when the object is null. |
ToOptional<T>(this T?) (class) |
Optional<T> |
Wraps a reference-type value as an Optional<T>. |
ToOptional<T>(this T?) (struct) |
Optional<T> |
Wraps a value-type value as an Optional<T>. |
ToOptionalAsync<T>(this Task<T?>, CancellationToken) (class) |
Task<Optional<T>> |
Asynchronously wraps a reference-type value as an Optional<T>. |
ToOptionalAsync<T>(this Task<T?>, CancellationToken) (struct) |
Task<Optional<T>> |
Asynchronously wraps a value-type value as an Optional<T>. |
ToSuccess<T>(this T) |
Success<T> |
Wraps the current value as a Success<T> result. |
ToSuccessAsync<T>(this Task<T>, CancellationToken) |
Task<Success<T>> |
Asynchronously wraps the current value as a Success<T> result. |
TryGetNonNull<T>(this T?, out T) |
bool |
Attempts to extract a non-null value from the current nullable object. |
public static T Apply<T>(this T value, Action<T> action) where T : classCalls the specified Action<T> with the current value, then returns the value unchanged. Useful for configuring a reference-type object in a fluent chain.
| Parameter | Type | Description |
|---|---|---|
value |
T |
The value to pass to the action. |
action |
Action<T> |
The action into which value is passed. |
Returns — The current value, after the action has executed.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
action is null. |
var list = new List<int>().Apply(items => items.Add(1)).Apply(items => items.Add(2));
// list contains { 1, 2 }public static T Apply<T>(this T value, Func<T, T> function) where T : structCalls the specified Func<T, T> with the current value-type value and returns its result. This overload is constrained to value types.
| Parameter | Type | Description |
|---|---|---|
value |
T |
The value to pass to the function. |
function |
Func<T, T> |
The function into which value is passed. |
Returns — The result of function.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
function is null. |
int result = 10.Apply(value => value * 2); // 20public static TResult Let<TSource, TResult>(this TSource value, Func<TSource, TResult> function)Calls the specified Func<TSource, TResult> with the current value as its argument and returns the result, transforming the value into a new type.
| Parameter | Type | Description |
|---|---|---|
value |
TSource |
The value to pass to the function. |
function |
Func<TSource, TResult> |
The function into which value is passed. |
Returns — The result of function.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
function is null. |
int length = "Hello".Let(text => text.Length); // 5public static string ToRecordString(this object? value)Gets a record-like string representation of the current object instance, listing its public instance properties as Name = Value pairs inside braces. Returns the literal "null" when value is null.
Note This method is designed specifically for record-like objects and may produce undesirable results when applied to primitive-like objects.
| Parameter | Type | Description |
|---|---|---|
value |
object? |
The current object instance. |
Returns — A record-like string representation of the current object instance.
var point = new { X = 1, Y = 2 };
string text = point.ToRecordString(); // e.g. "<>f__AnonymousType { X = 1, Y = 2 }"public static string ToStringOrNull(this object? value)Obtains a string representation of the current object, or the string literal "null" if the current object is null.
| Parameter | Type | Description |
|---|---|---|
value |
object? |
The current object instance. |
Returns — The result of value.ToString(), or "null" when value is null.
public static Optional<T> ToOptional<T>(this T? value) where T : notnullObtains an Optional<T> representation of the current reference-type object.
| Parameter | Type | Description |
|---|---|---|
value |
T? |
The object to wrap as an Optional<T> value. |
Returns — An Optional<T> representation of the current object.
Optional<string> some = "value".ToOptional();
Optional<string> none = ((string?)null).ToOptional();public static Optional<T> ToOptional<T>(this T? value) where T : structObtains an Optional<T> representation of the current value-type object.
| Parameter | Type | Description |
|---|---|---|
value |
T? |
The object to wrap as an Optional<T> value. |
Returns — An Optional<T> representation of the current object.
Optional<int> some = ((int?)42).ToOptional();public static async Task<Optional<T>> ToOptionalAsync<T>(this Task<T?> value, CancellationToken cancellationToken = default)
where T : notnullAsynchronously obtains an Optional<T> representation of the current reference-type object produced by the awaited task.
| Parameter | Type | Description |
|---|---|---|
value |
Task<T?> |
The task whose result is wrapped as an Optional<T> value. |
cancellationToken |
CancellationToken |
The token to monitor for cancellation requests. |
Returns — A task that produces an Optional<T> representation of the awaited result.
public static async Task<Optional<T>> ToOptionalAsync<T>(this Task<T?> value, CancellationToken cancellationToken = default)
where T : structAsynchronously obtains an Optional<T> representation of the current value-type object produced by the awaited task.
| Parameter | Type | Description |
|---|---|---|
value |
Task<T?> |
The task whose result is wrapped as an Optional<T> value. |
cancellationToken |
CancellationToken |
The token to monitor for cancellation requests. |
Returns — A task that produces an Optional<T> representation of the awaited result.
public static Success<T> ToSuccess<T>(this T value)Obtains a Success<T> representation of the current object.
| Parameter | Type | Description |
|---|---|---|
value |
T |
The object to wrap as a Success<T> result. |
Returns — A Success<T> representation of the current object.
Success<int> success = 42.ToSuccess();public static async Task<Success<T>> ToSuccessAsync<T>(this Task<T> value, CancellationToken cancellationToken = default)Asynchronously obtains a Success<T> representation of the value produced by the awaited task.
| Parameter | Type | Description |
|---|---|---|
value |
Task<T> |
The task whose result is wrapped as a Success<T> result. |
cancellationToken |
CancellationToken |
The token to monitor for cancellation requests. |
Returns — A task that produces a Success<T> representation of the awaited result.
public static bool TryGetNonNull<T>(this T? value, [NotNullWhen(true)] out T result)Attempts to extract a non-null value from the current nullable object. When the method returns true, result is guaranteed non-null (annotated with [NotNullWhen(true)]).
| Parameter | Type | Description |
|---|---|---|
value |
T? |
The current nullable object to test for nullability. |
result |
out T |
The non-null value when this method returns true; otherwise, null. |
Returns — true if the current nullable object is not null; otherwise, false.
string? maybe = GetValueOrNull();
if (maybe.TryGetNonNull(out string value))
{
Console.WriteLine(value.Length);
}- OnixLabs.Core
- Home
- OnixLabs.Core.Optional
- OnixLabs.Core.Result<T>
- OnixLabs.Core.ArrayExtensions
- OnixLabs.Core.DateTimeExtensions
- OnixLabs.Core.ComparableExtensions
- OnixLabs.Core.ISpanParsableExtensions
- OnixLabs.Core.RandomExtensions
- OnixLabs.Core.ReadOnlySequenceExtensions
- OnixLabs.Core.ReadOnlySpanExtensions
- OnixLabs.Core.StringExtensions
- OnixLabs.Core on NuGet
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions