c# - 将不同类型的 Action<T> 数组传递给方法

标签 c# generics

我有以下代码,它根据作为参数传递的值的类型存储要执行的操作列表...

Action<int> intAction = x => Console.WriteLine($"You sent an int: {x}");
Action<bool> boolAction = x => Console.WriteLine($"You sent a bool: {x}");

var funcsByInputType = new Dictionary<Type, Action<object>>();
funcsByInputType[typeof(int)] = (object x) => intAction((int)x);
funcsByInputType[typeof(bool)] = (object x) => boolAction((bool)x);

PerformAction(true);
PerformAction(42);

void PerformAction<TValue>(TValue value)
{
    if (funcsByInputType!.TryGetValue(typeof(TValue), out Action<object>? action))
    {
        action(value!);
    }
}

这按预期工作。

我现在想要做的是从我只调用一次的单个方法创建字典,这样我就可以像这样重写我的代码。

Action<int> intAction = x => Console.WriteLine($"You sent an int: {x}");
Action<bool> boolAction = x => Console.WriteLine($"You sent a bool: {x}");

funcsByInputType = MakeFuncsByInputType(intAction, boolAction);

PerformAction(true);
PerformAction(42);

void PerformAction<TValue>(TValue value)
{
    if (funcsByInputType!.TryGetValue(typeof(TValue), out Action<object>? action))
    {
        action(value!);
    }
}

如何编写 MakeFuncsByInputType 方法?

请注意,我不想多次调用该方法,也不想将我的参数定义为 params object[]

最佳答案

这是不可能的,因为通过 Action<object>作为 Action<string> 的别名我将能够通过 42而不是字符串。

所以我不得不改用构建器模式。

公共(public)静态部分类 Reducer { public static Builder New() => new Builder();

public class Builder<TState>
{
    private bool Built;
    private ImmutableArray<KeyValuePair<Type, Func<TState, object, Result<TState>>>> TypesAndReducers;

    internal Builder() => TypesAndReducers = ImmutableArray.Create<KeyValuePair<Type, Func<TState, object, Result<TState>>>>();

    public Builder<TState> Add<TDelta>(Func<TState, TDelta, Result<TState>> reducer)
    {
        if (reducer is null)
            throw new ArgumentNullException(nameof(reducer));

        EnsureNotBuilt();
        TypesAndReducers = TypesAndReducers.Add(new(typeof(TDelta), (state, delta) => reducer(state, (TDelta)delta)));
        return this;
    }

    public Func<TState, object, Result<TState>> Build()
    {
        EnsureNotBuilt();
        if (TypesAndReducers.Length == 0)
            throw new InvalidOperationException("Must add at least one reducer to build.");

        Built = true;

        var dictionary = TypesAndReducers
            .GroupBy(x => x.Key)
            .ToDictionary(x => x.Key, x => x.Select(x => x.Value));

        return (TState state, object delta) =>
        {
            if (delta is null)
                throw new ArgumentNullException(nameof(delta));

            if (!dictionary.TryGetValue(delta.GetType(), out var reducers))
                return (false, state);

            bool anyChanged = false;
            TState newState = state;
            foreach(var reducer in reducers)
            {
                (bool changed, newState) = reducer(newState, delta);
                anyChanged |= changed;
            }

            return anyChanged
                ? (true, newState)
                : (false, state);
        };
    }

    private void EnsureNotBuilt()
    {
        if (Built)
            throw new InvalidOperationException("Reducer has already been built.");
    }

}

关于c# - 将不同类型的 Action<T> 数组传递给方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71800311/

相关文章:

c# - 在 Monodroid 上与 GoogleMaps 叠加

c# - 将 JavaScriptConverter 转换为 json.net JsonConverter

c# - 如何使用 Helix.SharpDX 体积渲染可视化 3D 标量场?

generics - 如何使用 Guice 注入(inject)通用参数?

java - 使用无限通配符会捕获错误的情况,但只有在使用原始类型时才会被标记为警告

c# - 这个用于构造基本 Windows.Form 的简单 F# 代码的 C# 版本是什么?

c# - 如何从表中获取列的子集而不需要 Linq 中的新类型

typescript - 是否可以在 TypeScript 中使用模板文字生成字符串文字组合?

java - 泛型类型转换

java - 如何将通用列表转换为数组?