"Using generic type 'Func<TResult>' requires 1 type arguments" 定义具有 4 个以上参数的委托函数类型时出现问题

"Using generic type 'Func<TResult>' requires 1 type arguments" issue when defining a delegate function type with more than 4 arguments

我收到以下错误:"Using generic type 'Func<TResult>' requires 1 type arguments". It occurs when I attempt to define a dictionary which maps strings to delegate functions.

字典看起来像这样:

Dictionary<string, string> builtInFunctions = new Dictionary<string, Func<Expression, Dictionary<string, string>, Dictionary<string, Value>, Dictionary<string, Token>, Dictionary<string, Cube>, Result>>()
{
    {"ToString", ToString}
};

Result ToString(
    Expression expression,
    Dictionary<string, string> env, 
    Dictionary<string, Value> store, 
    ref Dictionary<string, Token> tokenEnv, 
    ref Dictionary<string, Cube> cubeEnv
) {
    // implemented ToString function
}

错误发生在这部分代码上:

Func<Expression, Dictionary<string, string>, Dictionary<string, Value>, Dictionary<string, Token>, Dictionary<string, Cube>, Result>

如果我为它使用不同的更简单的类型,它仍然会出现,例如:

Func<int, int, int, int, int, int>

委托函数是否只能接受 4 个参数,或者有什么办法解决这个问题?

您的 ToString 函数不能有 ref 个参数。

它们没有在您的 Func 中定义,也不可能。

查看 this question 了解更多信息。

您的代码可能有错误,因为用四个或五个参数声明 Func 应该不是问题。

这段代码对我来说工作得很好:

using System;
using System.Collections.Generic;

public class Expression{}
public class Value{}
public class Token{}
public class Cube{}
public class Result{
    public int result = 11;
}

namespace MyNameSpace{
    using MyGenericFunc = Func<Expression, Dictionary<string, string>, Dictionary<string, Value>, Dictionary<string, Token>, Dictionary<string, Cube>, Result>;


    public class Program
    {
        public static void Main()
        {
            Expression expression = new Expression();
            Dictionary<string, string> env = new Dictionary<string, string>(); 
            Dictionary<string, Value> store = new Dictionary<string, Value>();
            Dictionary<string, Token> tokenEnv = new Dictionary<string, Token>();
            Dictionary<string, Cube> cubeEnv = new Dictionary<string, Cube>();

            // normal call
            Console.WriteLine("When calling the method directly: " + ToStringFunc(expression, env, store, tokenEnv, cubeEnv).result);

            // call via func and dictionary
            MyGenericFunc retrievedFunc;
            builtInFunctions.TryGetValue("ToString", out retrievedFunc);
            Console.WriteLine("When calling the method retrieved from dictionary: " + retrievedFunc.Invoke(expression, env, store, tokenEnv, cubeEnv).result);
        }

        static Dictionary<string, MyGenericFunc> builtInFunctions = new Dictionary<string, MyGenericFunc>()
        {
            {"ToString", ToStringFunc}
        };

        static Result ToStringFunc(
            Expression expression,
            Dictionary<string, string> env, 
            Dictionary<string, Value> store, 
            Dictionary<string, Token> tokenEnv, 
            Dictionary<string, Cube> cubeEnv
        ) {
            return new Result();
        }
    }
}

你可以快速测试一下here