Storing a method as a member variable of a class

后端 未结 4 500
野趣味
野趣味 2020-12-11 14:58

I have this as one of my members of the class \'KeyEvent\':

private delegate void eventmethod();

And the constructor:

publi         


        
4条回答
  •  轮回少年
    2020-12-11 15:32

    You pretty much have the code already. Just create a member field of the right delegate type and save the parameter to it just like you would with any other data type.

    private eventmethod MySavedEvent;
    
    public void KeyEvent(eventmethod D) {
        // Save the delegate
        MySavedEvent = D;
    }
    
    public void CallSavedEvent() {
        if (MySavedEvent != null) {
            MySavedEvent();
        }
    }
    

提交回复
热议问题