.NET 4.0 框架中是否已存在以下通用方法?

发布于 2024-09-28 15:49:19 字数 626 浏览 0 评论 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 技术交流群。

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

发布评论

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

评论(4

玩物 2024-10-05 15:49:19

您可以在代码中使用 let 语法:

string test = "12,23,34,23,12";
var res = from string s in test.Split(',') 
          let u = s+s
          select int.Parse(u);

或者,您可以直接使用 LINQ 扩展方法而不是特殊语法:

string test = "12,23,34,23,12";
var res = test.Split(',')
              .Select(s => { var u = s + s; return int.Parse(u); });

自从问题更新后:

我不这样做意思是不尊重,但我认为这个解决方案没有必要。

这里有一些探索:

如果我们想接受像你所说的真正“任意”的 lambda,那么它们可以来自外部源,而 Wrap 不执行任何操作,因为它与 f 相同()

// with 'f' as some arbitrary lambda, then this:
var res = from string s in test.Split(',')
          select f.Wrap();

// is the same as:
var res = from string s in test.Split(',')
          select f();

但是如果你这样做,f就不能以任何方式依赖于s(例如,这样你就不能编写你的例子code):

// e.g. where does s come from?
var f = () => { var u = s+s; return int.Parse(u); }; 

// we can write it like this, as a function of 's':
var f2 = s => { var u = s+s; return int.Parse(u); };
//but then you can just use "select f2(s)" and we're back not needing Wrap any more

我们真正寻找的是 s 上的任意闭包。为此,必须内联定义 lambda,其中 s 在范围内,因此您不再真正接受真正的“任意”lambda,并且必须将它们直接写入代码。

这就是我提出 let 语法的原因,因为您能想到的任何 lambda 都可以转换为该语法,并且它与查询语法的其余部分相匹配。这就是 let 的设计目的! :)

或者,您也可以只使用带有像上面的 f2 这样的参数的 lambda。

如果您确实想坚持使用 lambda 语法,我建议使用扩展方法。就像我在评论中所说的那样,看起来您正在寻找介于查询和查询之间的东西。扩展语法。

我很想知道为什么要在查询语法中使用 lambda 语法?

希望这有帮助:)

You can use the let syntax in your code:

string test = "12,23,34,23,12";
var res = from string s in test.Split(',') 
          let u = s+s
          select int.Parse(u);

Alternately, you could use the LINQ extension methods directly instead of the special syntax:

string test = "12,23,34,23,12";
var res = test.Split(',')
              .Select(s => { var u = s + s; return int.Parse(u); });

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 as f():

// with 'f' as some arbitrary lambda, then this:
var res = from string s in test.Split(',')
          select f.Wrap();

// is the same as:
var res = from string s in test.Split(',')
          select f();

But if you do this, f can't depend upon s in any way (for example, this way you can't write your example code):

// e.g. where does s come from?
var f = () => { var u = s+s; return int.Parse(u); }; 

// we can write it like this, as a function of 's':
var f2 = s => { var u = s+s; return int.Parse(u); };
//but then you can just use "select f2(s)" and we're back not needing Wrap any more

What we're really looking for is arbitrary closures over s. For this to happen, the lambdas have to be defined inline, where s 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 what let 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 :)

你如我软肋 2024-10-05 15:49:19

这与 有何不同

var res = test.Split(',').Select(s => {
                                        var u = s + s;
                                        return int.Parse(u);
                                      });

是的,这可以编译并运行。

How is this different from

var res = test.Split(',').Select(s => {
                                        var u = s + s;
                                        return int.Parse(u);
                                      });

?

Yes, this compiles and runs.

梨涡 2024-10-05 15:49:19

根据 Reflector 的说法,这种方法不存在。但我不明白为什么你需要它:)

string test = "12,23,34,23,12";
var res = from string s in test.Split(',')
          select ((Func<int>)(() => { string u = s + s; return int.Parse(u); }))();

我认为这完全是一样的事情

According to Reflector, that method doesn't exist. But I don't see why you would need it :)

string test = "12,23,34,23,12";
var res = from string s in test.Split(',')
          select ((Func<int>)(() => { string u = s + s; return int.Parse(u); }))();

I think this would be exactly the same thing

染年凉城似染瑾 2024-10-05 15:49:19

不,它不存在。 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.

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