Table of Contents

Class Tweaker

Namespace
Cuemon
Assembly
Cuemon.Core.dll

Provides a way to change any instance of the same generic type.

public static class Tweaker
Inheritance
Tweaker

Examples

The following example demonstrates how to use Tweaker to apply inline transformations on values and objects: adjusting with a converter, altering in-place, and changing between types.

using System;
using System.Collections.Generic;

namespace Cuemon;

public class TweakerExample
{
    public void Demonstrate()
    {
        // Adjust: transform a value using a converter function
        int doubled = Tweaker.Adjust(21, x => x * 2);
        Console.WriteLine(doubled); // 42

        // Alter: modify an object in-place via an action delegate
        var list = new List<string> { "a", "b", "c" };
        Tweaker.Alter(list, lst => lst.Add("d"));
        Console.WriteLine(string.Join(", ", list)); // a, b, c, d

        // Change: convert a value to a different type
        string asString = Tweaker.Change(42, x => x.ToString());
        Console.WriteLine(asString); // 42

        // Adjust with null converter returns the original value unchanged
        int same = Tweaker.Adjust(10, null as Func<int, int>);
        Console.WriteLine(same); // 10
    }
}

Methods

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

Adjust the specified value with the function delegate converter.

public static T Adjust<T>(T value, Func<T, T> converter)

Parameters

value T

The 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>(T value, Action<T> modifier)

Parameters

value T

The 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).

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

Converts the specified value to a value of TResult.

public static TResult Change<T, TResult>(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.