事件和代表测试

发布于 2024-10-02 11:26:42 字数 3222 浏览 1 评论 0原文

请告诉我 Class1 和 Class2 之间或 Event 和 Delegate 之间有什么不同? 我在 Form1 中测试我的类并得到相同的结果

//Class1.cs 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用系统文本;

class Class1EventArgs
{
    public StringBuilder CallStack;

    public Class1EventArgs() {
        CallStack = new StringBuilder();
    }
}

delegate void Class1EventHandler(Object sender, Class1EventArgs e);

class Class1
{
    public event Class1EventHandler EventDelegate;

    public void EventCaller()
    {
        Class1EventArgs e = new Class1EventArgs();
        e.CallStack.AppendLine("EventCaller");
        OnClass1Event(e);
    }

    protected virtual void OnClass1Event(Class1EventArgs e)
    {
        if (EventDelegate != null)            
        {               
            EventDelegate(this, e);
            System.Windows.Forms.MessageBox.Show(e.CallStack.ToString());
        }
    }

}

//Class2.cs 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用系统文本;

public class Class2EventArgs
{
    public StringBuilder CallStack = new StringBuilder();
}

public delegate void Class2EventHandler(object sender,Class2EventArgs e);

class Class2
{
    public Class2EventHandler EventDelegate;

    public Class2()
    {
        EventDelegate = new Class2EventHandler(this.OnEventHappen);
    }

    public void EventCaller()
    {
        Class2EventArgs e = new Class2EventArgs();

        e.CallStack.AppendLine("EventCaller");

        EventDelegate.Invoke(this, e);

        System.Windows.Forms.MessageBox.Show(e.CallStack.ToString());
    }

    protected virtual void OnEventHappen(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("OnEventHappen");
    }

}

//Form1.cs 使用系统; 使用 System.Collections.Generic; 使用 System.ComponentModel; 使用系统数据; 使用系统绘图; 使用 System.Linq; 使用系统文本; 使用 System.Windows.Forms;

public partial class Form1 : Form
{
    Class1 c1 = new Class1();
    Class2 c2 = new Class2();

    public Form1()
    {
        InitializeComponent();
    }

    #region "Button1"      

    private void button1_Click(object sender, EventArgs e)
    {
        c1.EventCaller();
    }

    private void c1_EvenDelegate(object sender, Class1EventArgs e)
    {
        e.CallStack.AppendLine("c1_EvenDelegate");
    }

    private void c1_EvenDelegate_2(object sender, Class1EventArgs e)
    {
        e.CallStack.AppendLine("c1_EvenDelegate_2");
    }

    #endregion

    private void Form1_Load(object sender, EventArgs e)
    {
        //c1 = new Class1();
        c1.EventDelegate += new Class1EventHandler(c1_EvenDelegate);
        c1.EventDelegate += new Class1EventHandler(c1_EvenDelegate_2);

        c2.EventDelegate += new Class2EventHandler(c2_OnEventHappen1);
        c2.EventDelegate += new Class2EventHandler(c2_OnEventHappen2);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        c2.EventCaller();
    }

    private void c2_OnEventHappen1(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("c2_OnEventHappen1");
    }

    private void c2_OnEventHappen2(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("c2_OnEventHappen2");
    }
}

Please tell me what's the different between Class1 and Class2 or the different between Event and Delegate ?
I test my class in Form1 and have the same result

//Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Class1EventArgs
{
    public StringBuilder CallStack;

    public Class1EventArgs() {
        CallStack = new StringBuilder();
    }
}

delegate void Class1EventHandler(Object sender, Class1EventArgs e);

class Class1
{
    public event Class1EventHandler EventDelegate;

    public void EventCaller()
    {
        Class1EventArgs e = new Class1EventArgs();
        e.CallStack.AppendLine("EventCaller");
        OnClass1Event(e);
    }

    protected virtual void OnClass1Event(Class1EventArgs e)
    {
        if (EventDelegate != null)            
        {               
            EventDelegate(this, e);
            System.Windows.Forms.MessageBox.Show(e.CallStack.ToString());
        }
    }

}

//Class2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Class2EventArgs
{
    public StringBuilder CallStack = new StringBuilder();
}

public delegate void Class2EventHandler(object sender,Class2EventArgs e);

class Class2
{
    public Class2EventHandler EventDelegate;

    public Class2()
    {
        EventDelegate = new Class2EventHandler(this.OnEventHappen);
    }

    public void EventCaller()
    {
        Class2EventArgs e = new Class2EventArgs();

        e.CallStack.AppendLine("EventCaller");

        EventDelegate.Invoke(this, e);

        System.Windows.Forms.MessageBox.Show(e.CallStack.ToString());
    }

    protected virtual void OnEventHappen(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("OnEventHappen");
    }

}

//Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

public partial class Form1 : Form
{
    Class1 c1 = new Class1();
    Class2 c2 = new Class2();

    public Form1()
    {
        InitializeComponent();
    }

    #region "Button1"      

    private void button1_Click(object sender, EventArgs e)
    {
        c1.EventCaller();
    }

    private void c1_EvenDelegate(object sender, Class1EventArgs e)
    {
        e.CallStack.AppendLine("c1_EvenDelegate");
    }

    private void c1_EvenDelegate_2(object sender, Class1EventArgs e)
    {
        e.CallStack.AppendLine("c1_EvenDelegate_2");
    }

    #endregion

    private void Form1_Load(object sender, EventArgs e)
    {
        //c1 = new Class1();
        c1.EventDelegate += new Class1EventHandler(c1_EvenDelegate);
        c1.EventDelegate += new Class1EventHandler(c1_EvenDelegate_2);

        c2.EventDelegate += new Class2EventHandler(c2_OnEventHappen1);
        c2.EventDelegate += new Class2EventHandler(c2_OnEventHappen2);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        c2.EventCaller();
    }

    private void c2_OnEventHappen1(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("c2_OnEventHappen1");
    }

    private void c2_OnEventHappen2(object sender, Class2EventArgs e)
    {
        e.CallStack.AppendLine("c2_OnEventHappen2");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安人多梦 2024-10-09 11:26:42

事件是一个特殊的系统堆栈变量,它保存指向函数的指针。事件只能保存由委托定义的特殊类型的函数。
委托是函数的类型 - 它就像函数签名必须是什么样子的定义:该函数必须有 2 个 int 参数。这基本上就是代表对系统的意义。当您使用与其描述相匹配的函数实例化委托时,委托的实例现在保存该函数的地址。这是否有点清楚……?:)这就是我的看法。

所以基本上一个事件持有一个指向函数的预定义 void 类型(委托类型)的指针。

Event is a special system stack-like variable which holds pointer(s) to functions. An event can hold a only special type of functions as its defined by a delegate.
Delegate is type for a function - it's like a definition of what a function signature must look like: this function must have 2 int parameters. This is what basically a delegate means to the system. When you instantiate a delegate with a function which matches its description the instance of the delegate now holds address of that function. Is this clear somewhat or not yet ... ?:) This is how I see it.

So basically an event holds a pointer to pre-defined void type of function(the delegate type).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文