C# 如何将其他参数传递给MatchEvaluator
我有一段代码如下所示:C# 如何将其他参数传递给MatchEvaluator,c#,matchevaluator,C#,Matchevaluator,我有一段代码如下所示: text = reg.Replace(text, new MatchEvaluator(MatchEvalStuff)); text = reg.Replace(text, new MatchEvaluator(MatchEvalStuff, otherData)); 我需要传入第二个参数,如下所示: text = reg.Replace(text, new MatchEvaluator(MatchEvalStuff)); text = reg.Replace(te
text = reg.Replace(text, new MatchEvaluator(MatchEvalStuff));
text = reg.Replace(text, new MatchEvaluator(MatchEvalStuff, otherData));
我需要传入第二个参数,如下所示:
text = reg.Replace(text, new MatchEvaluator(MatchEvalStuff));
text = reg.Replace(text, new MatchEvaluator(MatchEvalStuff, otherData));
这可能吗?最好的方法是什么?MatchEvaluator是一个委托,因此您不能更改其签名。您可以创建一个委托,该委托使用附加参数调用方法。使用lambda表达式很容易做到这一点:
text = reg.Replace(text, match => MatchEvalStuff(match, otherData));
对不起,我应该提到我正在使用2.0,所以我没有访问lambdas的权限。以下是我最后做的:
private string MyMethod(Match match, bool param1, int param2)
{
//Do stuff here
}
Regex reg = new Regex(@"{regex goes here}", RegexOptions.IgnoreCase);
Content = reg.Replace(Content, new MatchEvaluator(delegate(Match match) { return MyMethod(match, false, 0); }));
这样我就可以创建一个“MyMethod”方法,并将需要的任何参数传递给它(param1和param2仅用于此示例,而不是我实际使用的代码)。非常感谢!我喜欢这个答案——基于这个解决方案的另一种实现方法
text=Regex.Replace(text,@“some_pattern”,新的matcheevaluator(match=>MatchEvalStuff(match,otherData))代码>对不起,我知道这是旧的,但有没有可能有人可以用代理声明来扩展这个答案,请,我不能绕着它转?