8

只是遇到以下代码行并且很难找到它的文档,是lambda expression吗?这是做什么的?

temp = Regex.Replace(url, REGEX_COOKIE_REPLACE,match => cookie.Values[match.Groups["CookieVar"].Value]);

特别感兴趣的=>

4

3 回答 3

9

如果您查看 Replace 的文档,第三个参数是MatchEvaluator

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx

这是一个以 aMatch作为参数并返回要替换它的字符串的委托。MatchEvaluator您的代码使用 lambda 表达式定义 a :

match => cookie.Values[match.Groups["CookieVar"].Value]

在这里,对于 Regex 找到的每个匹配项,都在字典中查找一个值,cookie.Values并将结果用作替换。

于 2011-05-17T09:22:38.200 回答
7
match => cookie.Values[match.Groups["CookieVar"].Value]

是一个捷径

delegate (Match match)
{
    return cookie.Values[match.Groups["CookieVar"].Value];
}
于 2011-05-17T09:22:41.640 回答
1

为in的RegEx.Replace每个匹配项运行 lambda,并用 lambdas 结果“替换”匹配项。REGEX_COOKIE_REPLACEurl

lambda(或速记委托)

match => cookie.Values[match.Groups["CookieVar"].Value]

使用 的Value"CookieVar"Group,在集合Match,中查找替换。cookie.Values查找值替换匹配项。

要告诉您有关“CookieVar”组的更多信息,我们需要查看对REGEX_COOKIE_REPLACE.

于 2011-05-17T09:36:08.253 回答