Table of Contents

ObjectExtensions Class

Definition

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

Extension methods for the Object class.

public static class ObjectExtensions
Inheritance
ObjectExtensions

Examples

ObjectExtensions provides extension methods for type conversion with fallback (As<T>), wrapper creation (UseWrapper), collection manipulation (Adjust, Alter), and delimited-string formatting. This example converts "42" to an int, handles "not-a-number" with a fallback of 99, wraps a string with UseWrapper to attach metadata, adjusts a List<int> by appending elements, and alters it in-place. Key steps include using the As method for safe casts, UseWrapper for attaching diagnostic data, and Adjust/Alter for immutable/mutable collection operations. Console output confirms each conversion result, the wrapper data value "example", hash codes, and the delimited string "1;2;3;4".

using System;
using System.Collections.Generic;
using Cuemon.Extensions;

namespace MyApp.Examples;

public static class ObjectExtensionsExample
{
    public static void Demonstrate()
    {
        object raw = "42";
        int number = raw.As(0);
        int fallback = "not-a-number".As(99);
        object nullValue = null;
        int safe = nullValue.As(0);
        string upper = "hello".As(value => value.ToUpperInvariant());
        object converted = raw.As(typeof(int));

        var wrapped = "docs".UseWrapper(extender =>
        {
            extender["source"] = "example";
        });

        var memberWrapped = 42.UseWrapper(typeof(int).GetMethod("ToString"), data =>
        {
            data["category"] = "number";
        });

        var numbers = new List<int> { 1, 2, 3 };
        var adjusted = numbers.Adjust(list =>
        {
            var copy = new List<int>(list);
            copy.Add(4);
            return copy;
        });
        var altered = numbers.Alter(list => list.Add(4));
        string delimited = adjusted.ToDelimitedString(options => options.Delimiter = ";");

        Console.WriteLine(number);
        Console.WriteLine(fallback);
        Console.WriteLine(safe);
        Console.WriteLine(upper);
        Console.WriteLine(converted);
        Console.WriteLine(wrapped.Data["source"]);
        Console.WriteLine(memberWrapped.MemberReference?.Name);
        Console.WriteLine(adjusted.GetHashCode32());
        Console.WriteLine(altered.GetHashCode64());
        Console.WriteLine(delimited);
        Console.WriteLine(default(int?).IsNullable<int?>());
    }
}

Methods

Name Description
Adjust<T>(T, Func<T, T>)

Adjust the specified value with the function delegate converter.

Alter<T>(T, Action<T>)

Adjust the specified value with the modifier delegate.

As(object, Type, Action<ObjectFormattingOptions>)

Attempts to converts the specified value to the given targetType.

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.

As<T, TResult>(T, Func<T, TResult>)

Converts the specified value to a value of TResult.

GetHashCode32<T>(IEnumerable<T>)

Computes a suitable hash code from the specified sequence of convertibles.

GetHashCode64<T>(IEnumerable<T>)

Computes a suitable hash code from the specified sequence of convertibles.

IsNullable<T>(T)

Determines whether the specified source is a nullable ValueType.

ToDelimitedString<T>(IEnumerable<T>, Action<DelimitedStringOptions<T>>)

Converts the specified source to a string of delimited values.

UseWrapper<T>(T, Action<IDictionary<string, object>>)

Wrap and extend an existing object of T with additional data.

UseWrapper<T>(T, MemberInfo, Action<IDictionary<string, object>>)

Wrap and extend an existing object of T with additional data.