C #: delegates and custom events

1. Overview of commission

"Request" corresponds to the C ++ "Function pointers" delegate must be consistent in the "parameters" and "return type" function to the "points" of;

// 定义Person类
public class Person {
    public string Name = "Rain Man";
    public string Speak(string words) {
        Console.WriteLine(this.Name + " said: " + words);
        return words;
    }
}

// 定义委托
public delegate string Dele_Speak(string str);

class Program {
    static void Main(string[] args) {
        Person p = new Person();                    // 实例化Person类
        Dele_Speak dp = new Dele_Speak(p.Speak);    // 实例化委托:变量dp实际上就是指向p.Speak函数的指针
        dp("Welcome to my blog!");                  // 输出:Rain Man said: Welcome to my blog!
        Console.ReadLine();
    }
}
  • Agent "Dele_Speak" and "Speak" method consistent parameter and return type;
  • "Dele_Speak dp = new Dele_Speak (p.Speak)", in fact, is to create a "dp" pointer "p.Speak" method
  • "Dp (" Welcome to my blog! ")", Is actually "p.Speak (" Welcome to my blog! ")"

2. Multicast

// 定义Person类
public class Person {
    public string Speak(string words) {
        Console.WriteLine("Speak: " + words);
        return "111";
    }
    public string Say(string words) {
        Console.WriteLine("Say: " + words);
        return "222";
    }
    public string Translate(string words) {
        Console.WriteLine("Translate: " + words);
        return "333";
    }
}
// 声明代理 public delegate string Dele_Str(string str); class Program { static void Main(string[] args) { Person p = new Person(); // 实例化Person类 Dele_Str dp_Speak = new Dele_Str(p.Speak); // 实例化委托指向 p.Speak Dele_Str dp_Say = new Dele_Str(p.Say); // 实例化委托指向 p.Say Dele_Str dp_Translate = new Dele_Str(p.Translate); // 实例化委托指向 p.Transpate // 多路广播 dp_Speak = dp_Speak + dp_Say; dp_Speak = dp_Speak + dp_Translate; string str = dp_Speak("Rain Man"); Console.WriteLine(str); // 输出:333 Console.ReadLine(); } }

Creates three functions in the Person class: Speak, Say, Translate, these three functions on the same parameters and return types, but use the same delegate (Dele_Str).

Multiple delegate: delegate using the same "point" to different functions, these functions may make "computing", which performs the following logic:

执行:
    string str = dp_Speak("Rain Man");
输出:
    Speak: Rain Man
    Say: Rain Man
    Translate: Rain Man

实际上就是执行下述代码:
    p.Speak("Rain Man");
    p.Say("Rain Man");
    p.Translate("Rain Man");

返回值:即最后一个函数的返回值

3. Event Agent

There are two forms:

  • FrmMain: This form has a button "btnAdd", when you click this button by ShowDialog () method to open "FrmUserAdd" form
  • FrmUserAdd: This form has a button "btnOK", when you click the button "External" (for FrmMain form) to send a "UserAddEvent" event by the event will "FrmUserAdd" fill in "user information" spread "frmMain" form.

3.1 FrmUserAdd form:

public partial class FrmUserAdd : Form 
{ // 1. 定义事件参数类 public class UserAddEventArgs : EventArgs { public User AddedUser; public UserAddEventArgs(User user) { this.AddedUser = user; } } // 2. 定义委托,并指定参数类型 public delegate void UserAddEventHandler(object sender, UserAddEventArgs e); // 3. 定义事件,并指定该事件的委托类型 public event UserAddEventHandler UserAddEvent; private void btnOK_Click(object sender, EventArgs e) { User user = new User(1, "Rain Man", ""); UserAddEventArgs args = new UserAddEventArgs(user); if (UserAddEvent != null) { this.UserAddEvent(this, args); } } }

3.1.1 custom event argument class: UserAddEventArgs

Since event arguments class definition "UserAddEventArgs" must inherit from the "EventArgs" category, on this basis, add a public member of the "AddedUser"

3.1.2 define delegates: UserAddEventHandler

  • Note the delegate type parameter, the second parameter is "event-defined parameters."
  • The commission is used to instantiate the "FrmMain" form, bind the event handler "OnUserAdd" instantiated.

3.1.3 custom event variables: UserAddEvent

"UserAddEvent" variable can be understood as "UserAddEventHandler" instance of a delegate object, i.e.,

public UserAddEventHandler UserAddEvent;    // 在该示例中把"event"修饰符去掉也是可以的

3.2 FrmMain form

public partial class FrmMain : Form {
    // UserAddEvent事件绑定的处理函数
    private void OnUserAdd(object sender, FrmUserAdd.UserAddEventArgs e) {
        MessageBox.Show(e.AddedUser.username);
    }

    private void btnAdd_Click(object sender, EventArgs e) {
        FrmUserAdd frm = new FrmUserAdd();
        FrmUserAdd.UserAddEventHandler dele_fn = new FrmUserAdd.UserAddEventHandler(OnUserAdd);

        frm.UserAddEvent += dele_fn;
        frm.ShowDialog();
    }
}

3.2.1 FrmUserAdd.UserAddEventHandler dele_fn = new FrmUserAdd.UserAddEventHandler(OnUserAdd);

dele_fn为“UserAddEventHandler”的一个实例(指针),它指向事件处理函数“OnUserAdd”

3.2.2 frm.UserAddEvent += dele_fn;

It can be seen here is in fact a "multicast" and also can be seen "UserAddEvent" event variable is actually an instance of delegate "UserAddEventHandler".

Execution logic 3.3

This example may seem complicated, the essence of this is to achieve "a window" in the, split into "two forms." The two following synthetic code form "a window"

public partial class FrmUserAdd : Form {
    // 定义事件参数
    public class UserAddEventArgs : EventArgs {
        public User AddedUser;
        public UserAddEventArgs(User user) {
            this.AddedUser = user;
        }
    }

    // 定义委托,并指定参数类型
    public delegate void UserAddEventHandler(object sender, UserAddEventArgs e);

    // 定义事件,并指定该事件的“委托”
    public UserAddEventHandler UserAddEvent;
    public event UserAddEventHandler UserAddEvent;
    
    // UserAddEvent事件绑定的处理函数
    private void OnUserAdd(object sender, FrmUserAdd.UserAddEventArgs e) {
        MessageBox.Show(e.AddedUser.username);
    }

    private void btnOK_Click(object sender, EventArgs e) {
        User user = new User(1, "Rain Man", "");
        UserAddEventArgs args = new UserAddEventArgs(user);

        FrmUserAdd.UserAddEventHandler dele_fn = new FrmUserAdd.UserAddEventHandler(OnUserAdd);
        this.UserAddEvent += dele_fn;

        if (UserAddEvent != null)
        {
            this.UserAddEvent(this, args);
        }
    }
}

Reproduced in: https: //www.cnblogs.com/rainman/p/3650925.html

Guess you like

Origin blog.csdn.net/weixin_33834137/article/details/93561364
Recommended