简体   繁体   English

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

[英]Does the following generic method already exist in the .NET 4.0 framework?

Does the following generic function exist anywhere in the .NET 4.0 framework? .NET 4.0框架中是否存在以下通用功能? 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 (ie, lambda expressions embedded in the select clause of a LINQ query): 它允许以下构造(即,嵌入在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);});

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). 更新:对于所有质疑此解决方案的人们,请查看Onkelborg的答案,以了解将不带Lambda.Wrap()的lambda包括在内的过程(同时仍保持查询语法)。 Please do not eliminate the lambda. 请不要消除lambda。 This has to work for abitrary (value returning) lambdas . 这必须适用于任意(返回值)lambda Also, I am looking for a query syntax solution, only. 另外,我只在寻找查询语法解决方案。 Please do not convert the expression to fluent syntax , thus trivializing it. 不要将表达式转换为流利的语法以免琐碎。

You can use the let syntax in your code: 您可以在代码中使用let语法:

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: 或者,您可以直接使用LINQ扩展方法而不是特殊语法:

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() : 如果我们想接受您所说的真正的“任意” 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();

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): 但是,如果执行此操作,则f不会以任何方式依赖s (例如,这种方式无法编写示例代码):

// 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 . 我们真正要寻找的是对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. 为此,必须内联定义lambda,其中s在范围内,因此您不再真正接受真正的“任意” lambda,而必须直接在代码中编写它们。

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. 这就是为什么我提出let语法的原因,因为您可以提出的任何lambda都可以转换为该语法,并且与其余查询语法一起使用。 This is what let is designed for! 这就是let的目的! :) :)

Alternately, you could just use lambdas which take parameters like f2 above. 或者,您可以只使用带有上述f2参数的lambda。

If you really want to stick with the lambda syntax, I'd suggest using the extension methods. 如果您真的想使用lambda语法,建议您使用扩展方法。 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? 我对为什么要在查询语法中使用lambda语法感兴趣?

Hope this helps :) 希望这可以帮助 :)

How is this different from 这有什么不同

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

?

Yes, this compiles and runs. 是的,它将编译并运行。

According to Reflector, that method doesn't exist. 根据Reflector,该方法不存在。 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 我认为这是完全一样的

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. Onkelborg的解决方案是开箱即用的最佳解决方案,因此,如果您确实想在LINQ表达式中执行任意代码块,那么我将编写您建议的Wrap函数,以便获得C#类型推断并使其不那么难看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM