2013-03-23 30 views
-1

这里的想法是允许调用者为循环的每个循环实现AfterHTMLPieceHandler,我不知道它是不是正确的,或者它应该是一个事件或委托。老实说,我不知道区别,但这是另一个问题。如何向调用者实现公开事件,请参阅代码示例?

//? Should it be static? 
public static class PageScan 
{ 
    public delegate string AfterHTMLPieceHandler(); 

    public static string GetHTML(string url) 
    { 
     HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url); 
     wRequest.Method = "GET"; 
     HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse(); 
     StreamReader sReader = new StreamReader(wResponse.GetResponseStream()); 
     string html = sReader.ReadToEnd(); 
     sReader.Close(); 
     wResponse.Close(); 
     sReader.Dispose(); 
     return html; 
    } 

    public static void GetPiecesOfHtml(string html, string constantHtml) 
    { 
     while (html.IndexOf("http://portfoliopad.com/images/") > -1) 
     { 
      // it will retrieve a piece of html given the constantHtml and remove from html in order to break the loop in the end 
      //How Do I hit the event for each time one cycle of the loop ends? 
     } 
    } 
} 
+0

真棒,感谢您的downvote没有评论,你让stackoverflow更丰富 – RollRoll 2013-03-23 23:25:06

+0

你正在创建一个委托,但它不是指向任何事件,我可以看到..你看了起来如何做匿名代表或如何使用/创建代表..? – MethodMan 2013-03-23 23:54:49

回答

0

这是关于使用代表的一个很好的参考:Understanding Delegates。记下示例主要部分中的委托实例,以及它如何与委托所指向的方法绑定。我希望这有帮助。