Table of Contents

Class ActionExtensions

Namespace
Cuemon.Extensions
Assembly
Cuemon.Extensions.Core.dll

Extension methods for the Action delegates.

public static class ActionExtensions
Inheritance
ActionExtensions

Examples

The following example demonstrates applying the options pattern and factory initialization using the Configure<TOptions>(Action<TOptions>) and CreateInstance<T>(Action<T>) extension methods.

using System;
using Cuemon.Configuration;
using Cuemon.Extensions;

namespace MyApp.Examples;

public static class ActionExtensionsExample
{
    private sealed class MyOptions : IParameterObject
    {
        public string Delimiter { get; set; } = ",";

        public string Qualifier { get; set; } = "\"";
    }

    private sealed class MyService
    {
        public string ConnectionString { get; set; } = string.Empty;

        public int Timeout { get; set; } = 30;
    }

    public static void Demonstrate()
    {
        var options = new Action<MyOptions>(setup =>
        {
            setup.Delimiter = ";";
            setup.Qualifier = "'";
        }).Configure();

        var service = new Action<MyService>(factory =>
        {
            factory.ConnectionString = "Server=(localdb)\\MSSQLLocalDB;Database=Docs";
            factory.Timeout = 60;
        }).CreateInstance();

        Console.WriteLine($"{options.Delimiter} {options.Qualifier}");
        Console.WriteLine($"{service.ConnectionString} ({service.Timeout}s)");
    }
}

Methods

Configure<TOptions>(Action<TOptions>)

Provides a generic way to support the options pattern which enables using custom options classes to represent a group of related settings.

public static TOptions Configure<TOptions>(this Action<TOptions> setup) where TOptions : class, IParameterObject, new()

Parameters

setup Action<TOptions>

The delegate that will configure the public read-write properties of TOptions.

Returns

TOptions

A default constructed instance of TOptions initialized with the options of setup.

Type Parameters

TOptions

The type of the custom options class.

See Also
Configure<TOptions>(Action<TOptions>, Action<TOptions>, Action<TOptions>)

CreateInstance<T>(Action<T>)

Provides a generic way to initialize the default, parameterless constructed instance of T.

public static T CreateInstance<T>(this Action<T> factory) where T : class, new()

Parameters

factory Action<T>

The delegate that will initialize the public write properties of T.

Returns

T

A default constructed instance of T initialized with factory.

Type Parameters

T

The type of the class having a default constructor.