我的 c# 自定义委托行为很奇怪(<X> 匹配 <my custom delegate> 没有重载)

My c# custom delegate behaves weird (No overload for <X> matches <my custom delegate>)

好的,所以在阅读 Func<> didnt accept a ref keyword inside the type declaration 之后创建自定义函数委托之后,我最终得到了这个委托:

private delegate TResult RefFunc<T, out TResult>(ref T arg);

在我的小测试项目中。我还声明了这两个函数:

private static string FirstFunction(string arg) {
    return "";
}
private static string SecondFunction(string arg) {
    return "";
}

(自从发现问题后,真的只有这个功能内容!)
遵守上述委托并作为参数传递给此函数:

private static void SampleFunction(RefFunc<String, String> argOne, RefFunc<String, String> argTwo) { ... }

像这样(简体):

private static void Main(string[] args) {
    SampleFunction(FirstFunction, SecondFunction);
}

那个函数调用不会成功,因为“Conversion of 'method group' to '<class>.RefFunc<string, string>' isn't possible”,这是有道理的——我直接传递一个函数而不先把它变成一个委托——尽管我很确定我已经在某处看到与 Action<> Delegate 一起使用的相同语法。无论如何,我在调查问题时以多种方式修改了 Main() 函数中的调用方代码,但是 none 以下方法解决了问题。

SampleFunction(new RefFunc<String, String>(FirstFunction), ...); // Causes "No overload for FirstFunction matches delegate RefFunc<string, string>" at the first function parameter
SampleFunction(RefFunc FirstFunction, ...); // Was worth a try
RefFunc handler = FirstFunction; SampleFunction(handler, ...); // As described in docs. Causes "The usage of type "RefFunc<T, TResult>" (generic) requires 2 type arguments" at keyword RefFunc
RefFunc<String, String> handler = FirstFunction; SampleFunction(handler, ...); // Didn't help it. Causes the same error as with the first attempt
// and a few others

Docs
我终于决定求助于 Whosebug。由于我的函数显然遵循我创建的委托,所以我无法真正理解为什么 C# 认为它们不这样做。感谢您的帮助!
请注意,虽然我只显示与所导致错误相关的代码,但该项目非常小,所以我确定问题不在其他地方

Since my functions clearly adhere to the delegate I created

不幸的是,您的 FirstFunctionSecondFunction 都不是 RefFunc 类型,只是因为 T 在委托定义中被 ref 传递同时,您的实际功能中缺少 ref

要么你修改这两个函数

public class Program
{
    private delegate TResult RefFunc<T, out TResult>(ref T arg);

    private static string FirstFunction(ref string arg) {
       return "";
    }
    private static string SecondFunction(ref string arg) {
       return "";
    }

    private static void SampleFunction(
        RefFunc<String, String> argOne, RefFunc<String, String> argTwo) 
    { 
    }

    public static void Main()
    {
        SampleFunction(FirstFunction, SecondFunction);
    }
}

或者你放弃 ref

private delegate TResult RefFunc<T, out TResult>(T arg);

现在是

的正确类型
private static string SecondFunction(string arg) {
    return "";
}