委托在事件中的角色 [英] The role of the delegate in Events

查看:77
本文介绍了委托在事件中的角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我一直在用C#学习活动,因为我想创建更多的模块化程序,我的课程不需要知道我的表格。我的想法是,我的表格将通过我的班级实例提出的事件进行更新。



通过几本不同的书籍研究了这些,我想我已经掌握了一般的概念。我当然可以创建可以引发事件的类,我可以订阅这些事件并采取适当的措施。但是,我仍然对代表在活动过程中的作用感到困惑:



Hi all,

I have been studying events in C# because I want to create more modular programs where my classes don't have to be aware of my forms. The idea is that my forms will be updated via events raised by instances of my classes.

Having studied this via a few different books I think I'm getting to grips with the concept in general. I can certainly create classes that can raise events, and I can subscribe to those events and take appropriate actions. However, I remain confused about the role of the delegate in the events process:

delegate void MyEventHandler();

class Test
{
    public event MyEventHandler Results;

    public void OnResults()
    {
        if (Results != null)
            Results();
    }
}

namespace EventsExperiments
{
    class Program
    {
        static void Handler()
        {
            Console.WriteLine("Event received by program object");
            Console.ReadKey();
        }

        static void Main(string[] args)
        {
            Test event1 = new Test();
            event1.Results += Handler;

            event1.OnResults();
        }
    }
}





我明白在上面的简短摘录中我创造了一个委托调用MyEventHandler(),它将作为事件的引用(如指针)。然后我创建了一个名为Test的类,我已经让它能够引发代表接受的事件。



我的问题:



1.

关于委托,说每次引发事件都是正确的,这个委托将指向对象的处理程序方法订阅?并且,如果有多个对象订阅该事件,那么代理将依次指向他们的每个订阅处理程序方法?

如果这种理解是正确的,那么我在哪里看到重新分配代码在我的代码中?



也许我从根本上误解了一些东西,在这种情况下我希望有人可以让我直截了当。



干杯!



Brian



I understand that in the above short excerpt I have created a delegate called MyEventHandler() which is going to act as a reference (like a pointer) for the events. Then I have created a class called Test and I have given it the ability to raise events that the delegate will accept.

MY QUESTION:

1.
With regard to the delegate, is it correct to say that each time an event is raised, this delegate will point to the handler method of the object that subscribes? And, if there are multiple objects subscribing to the event, then the delegate will point to each one of their subscribed handler methods in turn?
If this understanding is correct, then where do I see the reassignment of the delegate in my code?

Perhaps I am fundamentally misunderstanding something, and in that case I hope someone can set me straight.

Cheers!

Brian

推荐答案

你的假设是正确的可以为活动分配多个代表。



代表是方法签名的描述。



实例化一个委托,你必须将它指向一个与委托描述相匹配的特定方法。



Visual Studio允许开发人员非常懒惰。分配事件的行:



You are correct in your assumptions an event can be assigned multiple delegates.

A delegate is a description of a method signature.

When you instantiate a delegate you have to point it to a specific method which matches the description of the delegate.

Visual studio allows developers to be quite lazy. The line where you assign the event:

event1.Results += Handler





实际上发生的事情是:





In reality what is happening is this:

event1.Results += new MyEventHandler(Handler)





您创建委托的实例,指向它你的方法并将它分配给事件。



关于生成具有有限依赖性的代码的更广泛的主题,我建议研究SOLID原则。



这些是一套设计原则,如果遵循这些原则,几乎可以保证您的代码尽可能少地依赖。



http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) [ ^ ]


这篇关于委托在事件中的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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