如何在 C# 中返回委托函数或 lambda 表达式?
我正在尝试编写一个方法来返回其自身的实例。 伪代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以这样做:
但这几乎没有用。你唯一能做的就是这样,这很奇怪:
You can do it like this:
But it's pretty much useless. The only thing you can really do is something like this, which is weird:
另一种方法是创建一个组合器。很简单,但你不能用泛型来做到这一点,因为它会无限倒退。您必须直接声明它:
也就是说,D 是一个接受 D 并返回 D 的委托。
这里有一些关于 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:
That is, D is a delegate that takes a D and returns a D.
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
这听起来很像迭代器。如果您可以将代码重构为:
其中
T
是您的输入类型,P
是您的结果类型。不完全相同,但它应该可以完成工作。编辑:如果您想要一个流畅的界面,则该模式几乎是一成不变的:您创建一个非静态类并从您调用的每个函数(不是静态的)返回它。静态类的非静态版本称为单例,您可以将其用于此模式。
This sounds a lot like an iterator. If you can refactor your code to be something like:
Where
T
is your input type andP
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.
我不确定你想要实现什么,但作为一项智力练习“我可以返回一个返回自身的方法吗?”以下工作原理:
就像您所说,您不能让该方法返回
Func
因为这意味着该委托的返回类型需要是返回的Func
返回一个Func
的Func
等等...我能看到的摆脱这种无限递归的唯一方法是让它返回一个
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:
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 aFunc
that returns aFunc
that returns aFunc
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...