c#构造函数事件

本文关键字:事件 构造函数 | 更新日期: 2023-09-27 18:15:35

我创建了一个类,当我通过表单创建一个雇员对象时,我想给出一个消息;

这是我的类,事件和委托

public delegate void ctorDel(); 
class Employee
{
    private int empID;
    private string empName;
    public event ctorDel myEvent;
    public Employee(int empID,string empName)
    {
        this.empID = empID;
        this.empName = empName;
        **if (myEvent != null)
        {
            myEvent();
        }**
    }

和form

  int id = Convert.ToInt16(textBox1.Text);
            string name = textBox2.Text;
            Employee emp = new Employee(id, name);
            emp.myEvent += new ctorDel(showMessage);

和功能

 public void showMessage()
        {
            MessageBox.Show("An employee is created");
        }

c#构造函数事件

你想要完成什么?你所尝试的不起作用的原因是因为你在actor之后附加了委托。一旦调用了"new Employee",该事件早就被解雇了。

如果你真的需要这样的事件,创建一个工厂类:

public delegate void EmpCreated();
public EmployeeFactory {
  public event EmpCreated myEvent;
  public Employee Create(int empId, string empName){
    var result = new Employee(empId, empName);
    if(myEvent != null) myEvent();
    return result;
  }
}

订阅工厂类上的事件,您将获得事件

在构造函数已经运行的之后附加事件

在构造函数中引发实例事件是没有意义的,因为由于实例的初始化尚未完成,因此不能将任何处理程序附加到事件上…

但是,您可以创建一个静态事件:
public static event ctorDel myEvent;
...
Employee.myEvent += new ctorDel(showMessage);

(但不要在每次创建Employee时都订阅事件,否则处理程序将被调用多少次,因为有实例…)

解决方案

这是一个通用的方法来解决你的问题

public class EventFactory
{
    public U Create<U, V>(V constructorArgs)
    {
        var instance = (U)Activator.CreateInstance(typeof(U), constructorArgs);
        OnCreated?.Invoke();
        return instance;
    }
    public delegate void CreatedEventHandler();
    public event CreatedEventHandler OnCreated;
}

你可以做

var ef = new EventFactory();
ef.OnCreated += myEventHandler; 
var instance = ef.Create<Employee>(employeeArgs);

. .更进一步

可以调整我的代码,以便在需要传递事件参数或构造函数无参数时提供更大的灵活性。我还没有测试过但是它应该在

的某个地方
public class EventFactory<T>
{
    public U Create<U, V>(V constructorArgs, T eventArgs)
    {
        var instance = (U)Activator.CreateInstance(typeof(U), constructorArgs);
        OnCreated?.Invoke(eventArgs);
        return instance;
    }
    public U Create<U>(T eventArgs)
    {
        return Create<U, object>(null, eventArgs);
    }
    public delegate void CreatedEventHandler(T args);
    public event CreatedEventHandler OnCreated;
}
public class EventFactory
{
    public U Create<U, V>(V constructorArgs)
    {
        var instance = (U)Activator.CreateInstance(typeof(U), constructorArgs);
        OnCreated?.Invoke();
        return instance;
    }
    public U Create<U>() where U : new()
    {
        var instance = new U();
        OnCreated?.Invoke();
        return instance;
    }
    public delegate void CreatedEventHandler();
    public event CreatedEventHandler OnCreated;
}

您可以在创建Employee时传递处理程序:

private Employee(ctorDel construcEvent)
{
    if (construcEvent != null)
        this.myEvent += construcEvent;
}
public Employee(int empID,string empName, ctorDel construcEvent)
    : this(construcEvent)
{
    this.empID = empID;
    this.empName = empName;
    if (myEvent != null)
    {
        myEvent();
    }
}

然后:

Employee emp = new Employee(id, name, new ctorDel(showMessage));

订阅此事件时,实例已经构造完成。我建议使用工厂模式来隐藏构造函数。

class EmployeeFactory
{
     public Employee Create(int id, string name)
     {
         Employee instance = new Employee(id, name);
         var handler = EmployeeCreated;
         if (handler != null)
         {
              EmployeeEventArgs e = new EmployeeEventArgs(instance);
              handler(e);    
         }
         return instance;
     }
     public event EventHandler<EmployeeEventArgs>  EmployeeCreated;
}

事件订阅:

  factory.EmployeeCreated += MyHandler;

实例建设:

  var emp = factory.Create(id, name);