Func 和 Action 与不同数量的构造函数参数不一致?
Inconsistency between Func and Action with different amount of constructor parameters?
我正在开发一个 twitch 聊天机器人,在我的 Command.cs class 中我遇到了一个奇怪的问题。我有一个采用 Action<string, User, Channel>
的构造函数和一个采用 Func<string, User, Channel, bool>
的构造函数。第一个构造函数应该用 Func
调用第二个构造函数,后者调用 Action
和 return 为真。当我在两个构造函数中使用 7 个参数时,这工作正常,但一旦我添加第 8 个参数,它就停止编译。
我得到的错误是 CS8030 "Anonymous function converted to a void returning delegate cannot return a value." 但在我添加第 8 个参数之前,代码编译正常
这两个片段之间的唯一区别是我添加了一个额外的 description
参数。
之前:
public class Command
{
public Command(string name, Action<string, User, Channel> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false)
: this(name, (m, u, c) => { action(m, u, c); return true; }, adminOnly, modOnly, ownerOnly, hasUserCooldown, cooldown)
{
}
public Command(string name, Func<string, User, Channel, bool> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false)
{
}
}
public class User { }
public class Channel { }
之后:
public class Command
{
public Command(string name, Action<string, User, Channel> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false, string description = null)
: this(name, (m, u, c) => { action(m, u, c); return true; }, adminOnly, modOnly, ownerOnly, hasUserCooldown, cooldown, description)
{
}
public Command(string name, Func<string, User, Channel, bool> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false, string description = null)
{
}
}
public class User { }
public class Channel { }
非常感谢。
在这两个版本中,您忘记将 allowOtherCommands
可选参数传递给第二种方法。虽然在第一种方法中它只是编译器无法捕获的逻辑错误,但在第二种情况下 description
参数导致编译器将调用映射到期望 Action
的同一构造函数,因此错误。
只需传递所有可选参数,问题就会解决:
public Command(string name, Action<string, User, Channel> action,
bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true,
TimeSpan? cooldown = null, bool allowOtherCommands = false, string description = null)
: this(name, (m, u, c) => { action(m, u, c); return true; },
adminOnly, modOnly, ownerOnly, hasUserCooldown,
cooldown, allowOtherCommands, description)
{
}
我正在开发一个 twitch 聊天机器人,在我的 Command.cs class 中我遇到了一个奇怪的问题。我有一个采用 Action<string, User, Channel>
的构造函数和一个采用 Func<string, User, Channel, bool>
的构造函数。第一个构造函数应该用 Func
调用第二个构造函数,后者调用 Action
和 return 为真。当我在两个构造函数中使用 7 个参数时,这工作正常,但一旦我添加第 8 个参数,它就停止编译。
我得到的错误是 CS8030 "Anonymous function converted to a void returning delegate cannot return a value." 但在我添加第 8 个参数之前,代码编译正常
这两个片段之间的唯一区别是我添加了一个额外的 description
参数。
之前:
public class Command
{
public Command(string name, Action<string, User, Channel> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false)
: this(name, (m, u, c) => { action(m, u, c); return true; }, adminOnly, modOnly, ownerOnly, hasUserCooldown, cooldown)
{
}
public Command(string name, Func<string, User, Channel, bool> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false)
{
}
}
public class User { }
public class Channel { }
之后:
public class Command
{
public Command(string name, Action<string, User, Channel> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false, string description = null)
: this(name, (m, u, c) => { action(m, u, c); return true; }, adminOnly, modOnly, ownerOnly, hasUserCooldown, cooldown, description)
{
}
public Command(string name, Func<string, User, Channel, bool> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false, string description = null)
{
}
}
public class User { }
public class Channel { }
非常感谢。
在这两个版本中,您忘记将 allowOtherCommands
可选参数传递给第二种方法。虽然在第一种方法中它只是编译器无法捕获的逻辑错误,但在第二种情况下 description
参数导致编译器将调用映射到期望 Action
的同一构造函数,因此错误。
只需传递所有可选参数,问题就会解决:
public Command(string name, Action<string, User, Channel> action,
bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true,
TimeSpan? cooldown = null, bool allowOtherCommands = false, string description = null)
: this(name, (m, u, c) => { action(m, u, c); return true; },
adminOnly, modOnly, ownerOnly, hasUserCooldown,
cooldown, allowOtherCommands, description)
{
}