【问题标题】:"Using generic type 'Func<TResult>' requires 1 type arguments" issue when defining a delegate function type with more than 4 arguments定义具有 4 个以上参数的委托函数类型时,“使用泛型类型 'Func<TResult>' 需要 1 个类型参数”问题
【发布时间】:2021-05-23 01:49:28
【问题描述】:

我收到以下错误:"Using generic type 'Func&lt;TResult&gt;' 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 个参数,还是有办法解决这个问题?

【问题讨论】:

标签: c# first-class-functions


【解决方案1】:

您的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

【讨论】:

  • 当然,但这并不能回答关于我所询问的错误的问题。
  • 为了澄清我的最后一条评论,我可以看到用这个答案完成我想要的另一种方法,但是如果你定义了一定数量的参数,它并没有说明 Func 声明给出错误。
  • @OliverHare 那你一定有一些语法错误吧?我编辑了我的答案并提供了没有ref 参数的示例代码。
  • 看来你是对的。很抱歉之前的混淆,我正在使用带有 C# 扩展名的 VScode,当我有超过 5 种类型的 Func&lt;&gt; 时它会引发错误,但是测试你的代码,我发现它在运行时仍然有效。如果参数太多,C# 扩展会误解 Func 的语法,这一定是个问题。谢谢。
猜你喜欢
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多