在接口方法定义中将函数作为参数传递

Nic*_*aro 0 c# parameters lambda interface .net-3.5

我目前正在尝试使用(可能)C# 的 Func 或 Action 类型来处理 lambda。

我想创建一个名为 IMyInterface 的接口,它定义了一个名为 CreateCRUD 的方法。这应该需要 5 个参数。第一个是字符串。接下来的四个是调用 create、read、update 和 delete 方法的函数。

interface IMyInterface
{
    void CreateCRUD(string name, Action<void> createFunc, Action<void> readFunc, Action<void> updateFunc, Action<void> deleteFunc);
}
Run Code Online (Sandbox Code Playgroud)

四个函数定义不应带任何参数且不返回任何内容。上面的代码不能编译。请指出我正确的方向。

D S*_*ley 5

改用非泛型Action

interface IMyInterface
{
    void CreateCRUD(string name, Action createFunc, Action readFunc, Action updateFunc, Action deleteFunc);
}
Run Code Online (Sandbox Code Playgroud)

  • “void is not a type”:严格来说这不是真的...... `typeof(void)` 返回 `System.Void`。但它不能用作泛型类型参数 (2认同)