A utility library for transforming values using delegates in C#.
DelegateTransform is a utility library that provides methods for transforming values using various delegate types in C#. It offers a clean and efficient way to apply transformations using ActionRef, Func, and FuncRef delegates, making your code more readable and functional.
- ActionRef Transformations: Modify values by reference using action delegates
- Func Transformations: Transform values using function delegates
- FuncRef Transformations: Transform values by reference using function delegates
- Fluent API: Chain multiple transformations together
- Generic Implementation: Works with any value or reference type
Install-Package ktsu.DelegateTransformdotnet add package ktsu.DelegateTransform<PackageReference Include="ktsu.DelegateTransform" Version="x.y.z" />The With method can be used with an ActionRef delegate to modify the input value by reference:
using ktsu.DelegateTransform;
int input = 5;
DelegateTransform.With(input, (ref int x) => x *= 2);
// input is now 10The With method can be used with a Func delegate to transform the input value:
using ktsu.DelegateTransform;
int input = 5;
int result = DelegateTransform.With(input, (int x) => x * 2);
// result is 10The With method can be used with a FuncRef delegate to transform the input value by reference:
using ktsu.DelegateTransform;
int input = 5;
DelegateTransform.With(input, (ref int x) => x *= 2);
// input is now 10You can chain multiple transformations for more complex operations:
using ktsu.DelegateTransform;
// Starting value
string input = "example";
// Chain of transformations
var result = DelegateTransform
.With(input, s => s.ToUpper()) // EXAMPLE
.With(s => s.Replace("EX", "**")) // **AMPLE
.With(s => s + " transformed"); // **AMPLE transformed
Console.WriteLine(result); // Outputs: **AMPLE transformedusing ktsu.DelegateTransform;
var person = new Person { Name = "John", Age = 30 };
// Transform using ActionRef
DelegateTransform.With(person, (ref Person p) => {
p.Name = p.Name.ToUpper();
p.Age += 1;
});
Console.WriteLine($"{person.Name}, {person.Age}"); // Outputs: JOHN, 31
// Transform using Func
var description = DelegateTransform.With(person, p =>
$"{p.Name} is {p.Age} years old");
Console.WriteLine(description); // Outputs: JOHN is 31 years oldThe main class providing transformation methods.
| Name | Parameters | Return Type | Description |
|---|---|---|---|
With<T> |
T value, Action<ref T> transform |
T |
Transforms a value by reference using an action delegate |
With<T, TResult> |
T value, Func<T, TResult> transform |
TResult |
Transforms a value using a function delegate |
With<T> |
T value, Func<ref T, T> transform |
T |
Transforms a value by reference using a function delegate |
With<T> |
ref T value, Action<ref T> transform |
void |
Transforms a reference to a value using an action delegate |
Using reference-based transformations (ActionRef, FuncRef) can be more performant for large structs as they avoid unnecessary copying:
// For large structs, using ref can be more efficient
var largeStruct = new LargeStruct(1000000);
// This avoids copying the struct
DelegateTransform.With(ref largeStruct, (ref LargeStruct s) => {
s.Process();
});You can easily extend DelegateTransform with your own delegate types:
// Define a custom delegate type
public delegate void MyCustomDelegate<T>(ref T value, string parameter);
// Extension method for DelegateTransform
public static class DelegateTransformExtensions
{
public static T WithCustom<T>(this T value, MyCustomDelegate<T> transform, string parameter)
{
T result = value;
transform(ref result, parameter);
return result;
}
}
// Usage
var result = 5.WithCustom((ref int x, string p) => {
x *= int.Parse(p);
}, "3");
// result is 15Contributions are welcome! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please make sure to update tests as appropriate and adhere to the existing coding style.
This project is licensed under the MIT License - see the LICENSE.md file for details.