I’m trying to make a part of my code more fluent.
I have a string extension that makes an HTTP request out of the string and returns the response as a string. So I can do something like…
string _html = "http://www.stackoverflow.com".Request();
I’m trying to write an extension that will keep trying the request until it succeeds. My signature looks something like…
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
}
I intend to call it something like…
string _html = "http://www.stackoverflow.com".Request.KeepTrying();
Alas, that doesn’t seem to work =). I tried making it into a lambda first but that doesn’t seem to work either.
string _html = (() => "http://www.stackoverflow.com".Request()).KeepTrying();
Is there a way to do what I’m trying to do while keeping the syntax fairly fluent?
Suggestions much appreciated.
Thanks.
You can’t use a method group for extension methods, or lambda expressions. I blogged about this a while ago.
I suspect you could cast to
Func<string>:but that’s pretty nasty.
One alternative would be to change
Request()to return a Func, and use:Or if you wanted to keep the
Requestmethod itself simple, just add aRequestFuncmethod:and then call: