2
所以我有一個異步的Web請求方法有回調:如何實現遞歸回調模式?
private void DoWebRequest(string url, string titleToCheckFor,
Action<MyRequestState> callback)
{
MyRequestState result = new MyRequestState();
// ...
// a lot of logic and stuff, and eventually return either:
result.DoRedirect = false;
// or:
result.DoRedirect = true;
callback(result);
}
(只是指出來了前面,我WebRequest.AllowAutoRedirect被設置爲false多種原因)
我不「T手之前知道有多少重定向可以期待,所以我開始:
private void MyWrapperCall(string url, string expectedTitle,
Action<MyRequestState> callback)
{
DoWebRequest(url, expectedTitle, new Action<MyRequestState>((a) =>
{
if (a.DoRedirect)
{
DoWebRequest(a.Redirect, expectedTitle, new Action<MyRequestState>((b) =>
{
if (b.DoRedirect)
{
DoWebRequest(b.Redirect, expectedTitle, callback);
}
}));
}
}));
}
現在我的腦子崩潰,我怎麼可能把這是一個反覆循環,這樣它的最後一個回調回如果不需要ReDirects,則爲原始呼叫者è?
嗯謝謝主席先生!真棒和優雅,它的工作原理以及... – 2012-04-27 15:04:34