在另一个类中创建方法在调用类时调用一个事件? [英] Making method in another class call an event in calling class when it's done?

查看:164
本文介绍了在另一个类中创建方法在调用类时调用一个事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要下载一些html,所以我在另一个类中调用void GetHTML。完成后,我想传递在调用类中应该提出的事件。我该怎么做?

I need to start a download of some html so I call void GetHTML in another class. When it's done I want to pass along what Event it should raise in the calling class. How could I do this?

所以看起来像这样:

public class Stuff
{
    public void GetHTML(string url, event to raise here)
    {
       //Do stuff then raise event
    }
}
public class Other
{
    public Other()
    {
        Stuff stuff = new Stuff();
        stuff.GetHTML(someUrl, somehow sending info that HTML_Done should be called);
    }
    void HTML_Done(string result, Event e)
    {
         //Do stuff with the result since it's done
    }
}

我意识到我不清楚我想做什么,我很乐意填写任何缺少的部分。

I realize I'm not super clear with what I want to do, I'd be glad to fill in any missing parts.

感谢任何建议!

推荐答案


事件订阅和通知



public class Stuff
{
    // Public Event to allow other classes to subscribe to.
    public event EventHandler GetHtmlDone = delegate { };

    public void GetHTML(string url)
    {
        //Do stuff

        // Raise Event, which triggers all method subscribed to it!
        this.GetHtmlDone(this, new EventArgs());
    }
}

public class Other
{
    public Other()
    {
        Stuff stuff = new Stuff();

        // Subscribe to the event.
        stuff.GetHtmlDone += new EventHandler(OnGetHtmlDone);

        // Execute
        stuff.GetHTML("someUrl");
    }

    void OnGetHtmlDone(object sender, EventArgs e)
    {
        //Do stuff with the result since it's done
    }
}

使用此模式允许更多订阅者。

Using this pattern allows many more subscribers.

您也不会将通知者 Stuff 类绑定到调用者其他类。

You also do not tie the notifier, the Stuff class, to the caller, the Other class.

您有订阅者或不是订阅者,与 Stuff 类没有任何差异。

You either have subscribers or not, no difference to the Stuff class.

Stuff 类不应该知道订阅者,它只应该提出一个暴露于订阅的事件。

The Stuff class should not know about the subscriber, it should merely raise an event it exposes for subscription.

编辑

由于 ctacke 在评论中正确指出,使用 this.GetHtmlDone这个,新的EventArgs()); 会导致一个例外,如果没有人订阅。

我更改了我的代码,以确保事件可以随时通过初始化我的安全事件处理者。

我像我一样使用它的方法(通过提高它)我确定只有良好的做法才能始终初始化你正在使用的。

EDIT
As ctacke pointed out correctly in the comments, raising the event using this.GetHtmlDone(this, new EventArgs()); will cause an exception if nobody has subscribed.
I changed my code above to ensure the event can be raised safely at all times by initialising my eventhandler.
As I'm always using it (by raising it) I'm sure it is only good practise to always initialise what you are using.

我可以在事件上添加一个空的检查处理程序,但在我个人的反对中,我不同意必须负责的东西类。我觉得事件应该总是被提出,因为它是负责任的事情。

I could have added a null check on the event handler but in my personal oppinion I do not agree with that having to be the responsibility of the stuffclass. I feel the event should always be raised as it is the "responsible" thing to do.

我发现这个线程在哪一种确认给我看起来似乎不错。

I found this thread on SO which kind of confirmed to me that it does not seem wrong to do so.

此外,我还运行代码分析代码,以确保我没有通过初始化EventHandler打破CA1805规则。 CA1805没有被提出,没有任何规则被破坏。

In addition I also run Code Analysis on that code to ensure I'm not breaking the CA1805 rule by initialising the EventHandler. CA1805 was not raised and no rules have been broken.

使用我的汽车类比的评论,我相信没有初始化事件处理器,提高它一直是一样的比说当汽车转弯时,只能使用你的指示器,如果有人正在观看,如果不打扰。你永远不知道有人在看,所以你也可以确保你一直这样做。

Using my car analogy from the comments, I believe not initializing the eventhandler and raising it all the time would be the same than saying "When turning a corner in your car only use your indicator if someone is watching and if not don't bother". You never know if anyone is watching, so you might as well make sure you always do it.

这只是我个人的喜好。其他任何人,请随时添加!= null检查,如果这是您的预料之中。

This is simply my personal preference. Anyone else, please do add the != null check at all times if that is how you prefere to do it.

非常感谢您的评论 ctacke 并指出这一点。我从中学到了很多东西。

我现在必须回到我的一些项目,并更新一些代码,以确保我的库不会崩溃,如果没有订阅我的事件。在我的任何测试中,我感到非常愚蠢,没有发现。

Thank you so much for the comments ctacke and for pointing this out. I learned quite a lot from this.
I will have to go back to some of my projects now and update some code to make sure my libraries are not crashing if nobody subscribes to my events. I feel quite silly not ever having caught that in any of my Tests.

这篇关于在另一个类中创建方法在调用类时调用一个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆