只是遇到以下代码行并且很难找到它的文档,是lambda expression
吗?这是做什么的?
temp = Regex.Replace(url, REGEX_COOKIE_REPLACE,match => cookie.Values[match.Groups["CookieVar"].Value]);
特别感兴趣的=>
。
只是遇到以下代码行并且很难找到它的文档,是lambda expression
吗?这是做什么的?
temp = Regex.Replace(url, REGEX_COOKIE_REPLACE,match => cookie.Values[match.Groups["CookieVar"].Value]);
特别感兴趣的=>
。
如果您查看 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
并将结果用作替换。
match => cookie.Values[match.Groups["CookieVar"].Value]
是一个捷径
delegate (Match match)
{
return cookie.Values[match.Groups["CookieVar"].Value];
}
为in的RegEx.Replace
每个匹配项运行 lambda,并用 lambdas 结果“替换”匹配项。REGEX_COOKIE_REPLACE
url
lambda(或速记委托)
match => cookie.Values[match.Groups["CookieVar"].Value]
使用 的Value
"CookieVar"Group,
在集合Match,
中查找替换。cookie.Values
查找值替换匹配项。
要告诉您有关“CookieVar”组的更多信息,我们需要查看对REGEX_COOKIE_REPLACE.