.NET 4.0 框架中是否已存在以下通用方法?
.NET 4.0 框架中的任何位置是否存在以下通用函数?如果确实如此,我想重用它,而不是自己编写它:
public static class Lambda
{
public static U Wrap<U>(Func<U> f)
{
return f();
}
}
它允许使用以下构造(即嵌入 LINQ 查询的 select 子句中的 lambda 表达式):
string test="12,23,34,23,12";
var res=from string s in test.Split(',')
select Lambda.Wrap(() => {string u=s+s; return int.Parse(u);});
更新:对于所有质疑此解决方案的人,请查看 Onkelborg 的答案看看如何在不使用 Lambda.Wrap() 的情况下包含 lambda(同时仍保持查询语法)。请不要消除 lambda。这必须适用于任意(返回值)lambda。另外,我只是在寻找查询语法解决方案。请不要将表达式转换为流畅的语法,从而使其变得无足轻重。
Does the following generic function exist anywhere in the .NET 4.0 framework? I would like to reuse it if it does rather than writing it myself:
public static class Lambda
{
public static U Wrap<U>(Func<U> f)
{
return f();
}
}
It allows for the following construct (i.e., lambda expressions embedded in the select clause of a LINQ query):
string test="12,23,34,23,12";
var res=from string s in test.Split(',')
select Lambda.Wrap(() => {string u=s+s; return int.Parse(u);});
Update: To all people questioning this solution, look at Onkelborg's answer to see what it would take to include the lambda without Lambda.Wrap() (while still maintaining query syntax). Please do not eliminate the lambda. This has to work for abitrary (value returning) lambdas. Also, I am looking for a query syntax solution, only. Please do not convert the expression to fluent syntax, thus trivializing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在代码中使用
let
语法:或者,您可以直接使用 LINQ 扩展方法而不是特殊语法:
自从问题更新后:
我不这样做意思是不尊重,但我认为这个解决方案没有必要。
这里有一些探索:
如果我们想接受像你所说的真正“任意”的 lambda,那么它们可以来自外部源,而
Wrap
不执行任何操作,因为它与f 相同()
:但是如果你这样做,
f
就不能以任何方式依赖于s
(例如,这样你就不能编写你的例子code):我们真正寻找的是
s
上的任意闭包。为此,必须内联定义 lambda,其中s
在范围内,因此您不再真正接受真正的“任意”lambda,并且必须将它们直接写入代码。这就是我提出
let
语法的原因,因为您能想到的任何 lambda 都可以转换为该语法,并且它与查询语法的其余部分相匹配。这就是let
的设计目的! :)或者,您也可以只使用带有像上面的
f2
这样的参数的 lambda。如果您确实想坚持使用 lambda 语法,我建议使用扩展方法。就像我在评论中所说的那样,看起来您正在寻找介于查询和查询之间的东西。扩展语法。
我很想知道为什么要在查询语法中使用 lambda 语法?
希望这有帮助:)
You can use the
let
syntax in your code:Alternately, you could use the LINQ extension methods directly instead of the special syntax:
Since the question was updated:
I don't mean to be disrespectful, but I think this solution isn't necessary.
Here's a bit of an exploration:
If we want to accept truly "arbitrary" lambdas like you say, then they can come from an outside source, and
Wrap
does nothing because it's the same asf()
:But if you do this,
f
can't depend upons
in any way (for example, this way you can't write your example code):What we're really looking for is arbitrary closures over
s
. For this to happen, the lambdas have to be defined inline, wheres
is in scope, so you aren't really accepting truly "arbitrary" lambdas any more, and they have to be written directly in the code.This is why I proposed the
let
syntax, since any lambda you can come up with can be converted to that syntax, and it goes with the rest of the query syntax. This is whatlet
is designed for! :)Alternately, you could just use lambdas which take parameters like
f2
above.If you really want to stick with the lambda syntax, I'd suggest using the extension methods. Like I said in my comment, it looks like you're looking for something halfway between query & extension syntax.
I'd be interested in why you want to use the lambda syntax with the query syntax?
Hope this helps :)
这与 有何不同
?
是的,这可以编译并运行。
How is this different from
?
Yes, this compiles and runs.
根据 Reflector 的说法,这种方法不存在。但我不明白为什么你需要它:)
我认为这完全是一样的事情
According to Reflector, that method doesn't exist. But I don't see why you would need it :)
I think this would be exactly the same thing
不,它不存在。 Onkelborg 的解决方案是开箱即用的最佳解决方案,因此,如果您确实想在 LINQ 表达式内执行任意代码块,那么我将编写您建议的
Wrap
函数,以获得C# 类型推断开始发挥作用并使其不那么难看。No, it does not exist. Onkelborg's solution is the best you can do out of the box, so if you do really want to execute an arbitrary code block inside a LINQ expression, then I would author the
Wrap
function you suggest in order to get C# type inference to kick in and make it less ugly.