我正在尝试使我的代码的一部分更加流畅。
我有一个字符串扩展,它从字符串发出 HTTP 请求并将响应作为字符串返回。所以我可以做类似的事情......
string _html = "http://www.stackoverflow.com".Request();
我正在尝试编写一个扩展,它将继续尝试请求直到它成功。我的签名看起来像...
public static T KeepTrying<T>(this Func<T> KeepTryingThis) {
// Code to ignore exceptions and keep trying goes here
// Returns the result of KeepTryingThis if it succeeds
}
我打算称它为...
string _html = "http://www.stackoverflow.com".Request.KeepTrying();
唉,这似乎不起作用=)。我试着先把它变成一个 lambda,但这似乎也不起作用。
string _html = (() => "http://www.stackoverflow.com".Request()).KeepTrying();
有没有办法在保持语法相当流畅的同时做我想做的事情?建议非常感谢。
谢谢。