如何向调用者实现公开事件,请参阅代码示例?

时间:2013-03-23 23:20:17

标签: c# .net oop delegates

这里的想法是允许调用者为循环的每个循环实现AfterHTMLPieceHandler,我不知道它是否正确,或者它应该是一个insted或delegate事件。老实说,我不知道区别,但这是另一个问题。

//? 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?
        }
    }
}

1 个答案:

答案 0 :(得分:0)

以下是使用委托的好参考:Understanding Delegates。请注意示例主要部分中的委托实例,以及它如何与委托指向的方法绑定。我希望这会有所帮助。