我在使用C#和泛型类型推断方面遇到了麻烦.我想编写一个方法来传递一个具有任何类型的方法,但编译器无法推断我传入的方法的类型.编译器总是抱怨消息
期待一种方法用'??? TestFunc(???,???)'签名
这是一个测试用例.
using System;
public class Example
{
private interface ITest
{
int TestFunc(string str, int i);
}
private class Test : ITest
{
public int TestFunc(string str, int i) { return 0; }
}
public static void Main()
{
ITest t = new Test();
DoWork(t.TestFunc);
}
public static void DoWork<T1, T2, TResult>(Func<T1, T2, TResult> func)
{
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释我的问题是什么?