如何使用现有函数并在C#中使用Func <..>编写它?

gra*_*ady 7 c# delegates

我写了这段代码:

public static bool MyMethod(int someid, params string[] types)
{...}
Run Code Online (Sandbox Code Playgroud)

我怎么能用Func写这个?

public static Func < int, ?params?, bool > MyMethod = ???
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 8

使用params关键字将关键字编译为普通参数ParamArray.您不能将属性应用于通用参数,因此您的问题是不可能的.

请注意,您仍然可以使用常规(非params)委托:

Func<int, string[], bool> MyMethodDelegate = MyMethod;
Run Code Online (Sandbox Code Playgroud)

为了将params关键字与委托一起使用,您需要创建自己的委托类型:

public delegate bool MyMethodDelegate(int someid, params string[] types);
Run Code Online (Sandbox Code Playgroud)

你甚至可以把它变成通用的:

public delegate TResult ParamsFunc<T1, T2, TResult>(T1 arg1, params T2[] arg2);
Run Code Online (Sandbox Code Playgroud)