如何在 C# 中返回委托函数或 lambda 表达式?

发布于 2024-11-03 22:16:50 字数 466 浏览 0 评论 0原文

我正在尝试编写一个方法来返回其自身的实例。 伪代码

Func<T,Func<T>> MyFunc<T>(T input)
{
    //do some work with input
    return MyFunc;
}

看起来很简单。但我在定义返回类型时遇到问题。 返回类型应该是一个委托

 which takes T as parameter, then returns a function 
 which takes T as parameter, then returns a function 
 which takes T as parameter, then returns a function

   ...recursive definition

,我确信有一些我没有注意到的微妙的事情。有人可以帮我指出吗? 谢谢。

I am trying to write a method to return an instance of itself.
The pseudo code is

Func<T,Func<T>> MyFunc<T>(T input)
{
    //do some work with input
    return MyFunc;
}

seems simple enough. But I am having problem defining the return type.
The return type should be a delegate

 which takes T as parameter, then returns a function 
 which takes T as parameter, then returns a function 
 which takes T as parameter, then returns a function

   ...recursive definition

I am sure there was some subtle thing that I didn't notice. Can someone point it out for me?
Thank you.

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

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

发布评论

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

评论(4

好久不见√ 2024-11-10 22:16:50

你可以这样做:

delegate F<T> F<T>(T obj);

F<T> MyFunc<T>(T obj)
{
    return MyFunc;
}

但这几乎没有用。你唯一能做的就是这样,这很奇怪:

void Main()
{
    MyFunc(1)(2)(3)(4);
}

delegate F<T> F<T>(T obj);

F<T> MyFunc<T>(T obj)
{
    Console.WriteLine(obj);
    return MyFunc;
}

You can do it like this:

delegate F<T> F<T>(T obj);

F<T> MyFunc<T>(T obj)
{
    return MyFunc;
}

But it's pretty much useless. The only thing you can really do is something like this, which is weird:

void Main()
{
    MyFunc(1)(2)(3)(4);
}

delegate F<T> F<T>(T obj);

F<T> MyFunc<T>(T obj)
{
    Console.WriteLine(obj);
    return MyFunc;
}
红颜悴 2024-11-10 22:16:50

另一种方法是创建一个组合器。很简单,但你不能用泛型来做到这一点,因为它会无限倒退。您必须直接声明它:

delegate D D(D d);

也就是说,D 是一个接受 D 并返回 D 的委托。

static D MyCombinator(D d)
{
    return MyCombinator;
}

这里有一些关于 C# 中组合器的更多想法:

http://blogs.msdn.com/b/ericlippert/archive/2006/06/23/standard-generic-delegate-types-part-two.aspx

Another way to do it is to make a combinator. Easy peasy, but you can't do it with generics because of that infinite regress. You've got to declare it directly:

delegate D D(D d);

That is, D is a delegate that takes a D and returns a D.

static D MyCombinator(D d)
{
    return MyCombinator;
}

A few more thoughts on combinators in C# here:

http://blogs.msdn.com/b/ericlippert/archive/2006/06/23/standard-generic-delegate-types-part-two.aspx

懷念過去 2024-11-10 22:16:50

这听起来很像迭代器。如果您可以将代码重构为:

IEnumerator<P> GetEnumerator<T,P>(T input)
{
  while(<some condition>)
  {
     // do some work with input
     yield return results; 
  }
}

其中 T 是您的输入类型,P 是您的结果类型。不完全相同,但它应该可以完成工作。

编辑:如果您想要一个流畅的界面,则该模式几乎是一成不变的:您创建一个非静态类并从您调用的每个函数(不是静态的)返回它。静态类的非静态版本称为单例,您可以将其用于此模式。

This sounds a lot like an iterator. If you can refactor your code to be something like:

IEnumerator<P> GetEnumerator<T,P>(T input)
{
  while(<some condition>)
  {
     // do some work with input
     yield return results; 
  }
}

Where T is your input type and P is your result type. Not identical, but it should get the work done.

Edit: If instead you want a fluent interface, the pattern is pretty much set in stone: you create a non-static class and return it from every function (which isn't static) you call. The non-static version of a static class is called a singleton and you can use it for this pattern.

仙女山的月亮 2024-11-10 22:16:50

我不确定你想要实现什么,但作为一项智力练习“我可以返回一个返回自身的方法吗?”以下工作原理:

object MyFunc<T>(T input)
{
    Func<T, object> returnValue = MyFunc;
    return returnValue;
}

就像您所说,您不能让该方法返回 Func 因为这意味着该委托的返回类型需要是返回的 Func返回一个 FuncFunc 等等...

我能看到的摆脱这种无限递归的唯一方法是让它返回一个 object相反,这要求调用者强制转换为正确的类型。

编辑:波吉斯的答案更好......

I'm not sure what you are trying to achieve, but as an intelectual exercise "can I return a method that returns itself?" the following works:

object MyFunc<T>(T input)
{
    Func<T, object> returnValue = MyFunc;
    return returnValue;
}

Like you say, you can't have the method return a Func as this would mean the return type of that delegate would need to be a Func that returns a Func that returns a Func etc...

The only way out of this infinite recusion that I can see is to have it return an object instead, which requires that the caller cast to the correct type.

Edit: Porges answer is better...

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