如何链接接受 Func<> 的方法重载代表们? (C#)

发布于 2024-09-28 07:27:47 字数 429 浏览 1 评论 0 原文

我有一个具有两个重载的方法,如下所示:

bool Evaluate(Func<bool> condition)
{
    // Some logic

    return condition.Invoke();
}

bool Evaluate<T>(Func<T, bool> condition, T value)
{
    // Same logic as the first method overload

    return condition.Invoke(value);
}

由于两个方法重载包含基本相同的逻辑,因此我希望将它们链接在一起,但我不知道如何执行此操作。我想第一个方法重载需要构造一个委托,并将其传递给第二个重载,但尚不清楚该委托应采用什么形式。

非常感谢您的建议,

蒂姆

I have a method with two overloads, as follows:

bool Evaluate(Func<bool> condition)
{
    // Some logic

    return condition.Invoke();
}

bool Evaluate<T>(Func<T, bool> condition, T value)
{
    // Same logic as the first method overload

    return condition.Invoke(value);
}

Since both method overloads contain largely identical logic, I wish to chain them together, but I can't see how to do this. I imagine the first method overoad needs to construct a delegate that it passes to the second overload, but it's not clear what form this delegate should take.

Many thanks for your advice,

Tim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ㄟ。诗瑗 2024-10-05 07:27:47

试试这个(您不需要调用 Invoke):

bool Evaluate(Func<bool> condition) {
  // logic ...
  return condition();
}

bool Evaluate<T>(Func<T, bool> condition, T value) {
  return Evaluate(() => condition(value));
} 

或者也许您的逻辑是可重用的,在它自己的方法中提取它可能是有意义的:

bool Evaluate(Func<bool> condition) {
  Logic();
  return condition();
}

bool Evaluate<T>(Func<T, bool> condition, T value) {
  Logic();
  return condition(value);
}

void Logic() {
  // logic...
} 

Try this (you don't need to call Invoke):

bool Evaluate(Func<bool> condition) {
  // logic ...
  return condition();
}

bool Evaluate<T>(Func<T, bool> condition, T value) {
  return Evaluate(() => condition(value));
} 

Or maybe your logic is reusable, it might make sense to extract it in its own method:

bool Evaluate(Func<bool> condition) {
  Logic();
  return condition();
}

bool Evaluate<T>(Func<T, bool> condition, T value) {
  Logic();
  return condition(value);
}

void Logic() {
  // logic...
} 
箹锭⒈辈孓 2024-10-05 07:27:47

像这样的事情:

bool Evaluate(Func<bool> condition)
{
    return Evaluate(p => condition.Invoke(), false);
}

Something like this:

bool Evaluate(Func<bool> condition)
{
    return Evaluate(p => condition.Invoke(), false);
}
阳光的暖冬 2024-10-05 07:27:47

最简单的方法是用接受并忽略单个参数的委托包装原始的无参数委托。

bool Evaluate(Func<bool> condition) 
{ 
    return Evaluate( _ => condition(), 0); 
} 

The easiest way is to wrap the original no parameter delegate with one that accepts and ignores a single parameter.

bool Evaluate(Func<bool> condition) 
{ 
    return Evaluate( _ => condition(), 0); 
} 
尹雨沫 2024-10-05 07:27:47

除非您要“组成”要传递的值,否则您的第一个重载无法调用第二个重载。不过,你的第二个可以轻松调用第一个。

bool Evaluate<T>(Func<T, bool> condition, T value)
{
   return Evaluate( () => condition(value));
}

Your first overload cannot call the second unless you are going to "make up" a value to pass. Your second can easily call the first, though.

bool Evaluate<T>(Func<T, bool> condition, T value)
{
   return Evaluate( () => condition(value));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文