另一个类的制作方法调用调用类的事件,当它做了什么? [英] Making method in another class call an event in calling class when it's done?

查看:155
本文介绍了另一个类的制作方法调用调用类的事件,当它做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开始一些HTML的下载,所以我调用另一个类无效GetHTML。当它这样做我想沿着它应在调用类提高什么事件经过。我怎么能做到这一点。



因此,这将是这个样子:

 公共类的东西
{
公共无效GetHTML(字符串URL,事件在这里提)
{
//做的东西再抬高事件
}
}
公共类其他
{
公共其他()
{
的东西的东西=新的东西();
stuff.GetHTML(someUrl,莫名其妙地发送信息的HTML_Done应该叫);
}
无效HTML_Done(字符串结果,事件E)
{
//做,结果东西,因为它做
}
}

我知道我不是什么我想要做的超清晰,我很乐意填写任何缺少的部分。



感谢您的任何建议!


解决方案

事件订阅和通知




 公共类的东西
{
//公共事件,让其他类订阅。
公共事件的EventHandler GetHtmlDone =委托{};

公共无效GetHTML(字符串URL)
{
//做的东西

//引发事件,这将触发订阅了它所有的方法!
this.GetHtmlDone(在此,新的EventArgs());
}
}

公共类其他
{
公共其他()
{
的东西的东西=新的东西() ;

//订阅事件。
stuff.GetHtmlDone + =新的EventHandler(OnGetHtmlDone);

//执行
stuff.GetHTML(someUrl);
}

无效OnGetHtmlDone(对象发件人,EventArgs五)
{
//做,结果东西,因为它做
}
}

使用这个模式可以让更多的用户。



您还没有扎发出通知,在的东西类,给调用者,在其他类。



您要么有用户与否,在的东西类没有区别。



的东西类不应该知道的订户,它应该仅仅是提高它公开认购的事件。



修改结果
作为 ctacke 的意见正确地指出,使用引发事件的 this.GetHtmlDone(这一点,新的EventArgs()); 将导致异常,如果没有人认购结果
我改变通过上面的初始化我的代码,以确保该事件可以安全地在任何时候都提高了我的。事件处理程序。结果
由于我一直在使用它(通过提高它)我敢肯定,这仅仅是很好的做法,总是初始化你使用的是什么。



我可以添加的事件处理程序,但在我个人oppinion一个空检查,我不与有成为的的东西类的责任达成一致。我觉得事件应始终提出,因为它是负责任的事。



我发现这个的上SO哪种向我证实它似乎并没有错这样做。



另外我还该代码运行代码分析,以确保我不会被初始化的事件处理程序打破了CA1805规则。 CA1805没有提出,也没有规则已被打破。



从注释中使用我的汽车来比喻,我相信没有初始化事件处理程序,提高它所有的时间将是相同的不是说当你的车转弯时只用你的指标,如果有人在看,如果没有不打扰。你永远不知道是否有人在看,所以你还不如确保您始终做到这一点。



这仅仅是我个人的偏好。任何人,请不要在任何时候加入!= NULL检查,如果这是你如何prefere做。



感谢您这么多的评论 ctacke 并指出了这一点。我学到了很多来源于此。结果
我现在必须回去我的一些项目,并更新了一些代码,以确保我的库没有崩溃,如果没有人赞同我的事件。我觉得挺傻不永远不必陷入在任何我的测试中。


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?

So it would look something like 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.

Thanks for any suggestions!

解决方案

Event Subscription and Notification

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.

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

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

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

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.

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.

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.

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天全站免登陆