[英]How do I return a delegate function or a lambda expression in c#?
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.谢谢你。
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;
}
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.很容易,但是由于无限的回归,你不能用 generics 做到这一点。 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.也就是说,D 是一个接受 D 并返回 D 的委托。
static D MyCombinator(D d)
{
return MyCombinator;
}
A few more thoughts on combinators in C# here:关于 C# 中组合子的更多想法:
http://blogs.msdn.com/b/ericlippert/archive/2006/06/23/standard-generic-delegate-types-part-two.aspx http://blogs.msdn.com/b/ericlippert/archive/2006/06/23/standard-generic-delegate-types-part-two.aspx
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.其中T
是您的输入类型, P
是您的结果类型。 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.编辑:如果您想要一个流畅的界面,那么该模式几乎是一成不变的:您创建一个非静态 class 并从您调用的每个 function (不是静态的)返回它。 The non-static version of a static class is called a singleton and you can use it for this pattern. static class 的非静态版本称为singleton ,您可以将其用于此模式。
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...就像您说的那样,您不能让该方法返回Func
,因为这意味着该委托的返回类型将需要是返回 Func 的Func
,该Func
返回一个Func
等...
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.我能看到的这种无限回避的唯一方法是让它返回一个object
,这需要调用者转换为正确的类型。
Edit: Porges answer is better...编辑:波吉斯的答案更好......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.