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