C# constructor event

后端 未结 6 769
南旧
南旧 2021-01-21 03:53

i created a class and when i create an employee object via form , i want to give a message;

this is my class, event and delegate

public delegate void cto         


        
相关标签:
6条回答
  • 2021-01-21 04:16

    What is it you're trying to accomplish? The reason what you've tried doesn't work is because you're attaching your delegate after the ctor. Once you've called "new Employee" the event is long since fired.

    If you really need such an event, create a factory class:

    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;
      }
    }
    

    Subscribe to the event on the factory class and you'll get the event.

    0 讨论(0)
  • 2021-01-21 04:19

    You can pass the handler when creating the 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();
        }
    }
    

    And then:

    Employee emp = new Employee(id, name, new ctorDel(showMessage));
    
    0 讨论(0)
  • 2021-01-21 04:26

    Solution

    Here's a generic approach to your problem

    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;
    }
    

    You can then do

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

    .. Going further

    It is possible to adjust my code to provide greater flexiblity when you need to pass event arguments or when the constructor is parameterless. I haven't tested it but it should look somewhere along the lines of

    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;
    }
    
    0 讨论(0)
  • 2021-01-21 04:33

    By the time you subscribe for this event the instance is already constructed. I would recommend using Factory pattern to hide the constructor.

    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;
    }
    

    Event subscription:

      factory.EmployeeCreated += MyHandler;
    

    Instance construction:

      var emp = factory.Create(id, name);  
    
    0 讨论(0)
  • 2021-01-21 04:34

    You're attaching the event after the constructor has already run.

    0 讨论(0)
  • 2021-01-21 04:36

    It doesn't make sense to raise an instance event in the constructor, because since the initialization of the instance is not yet complete, there can't be any handler attached to the event...

    However, you could create a static event:

    public static event ctorDel myEvent;
    
    ...
    
    Employee.myEvent += new ctorDel(showMessage);
    

    (but don't subscribe to the event every time you create an Employee, or the handler will be invoked as many times as there are instances...)

    0 讨论(0)
提交回复
热议问题