本文介绍了将方法存储为类的成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我将此作为我的"KeyEvent"类的成员之一:

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

private delegate void eventmethod();

构造函数:

public KeyEvent(eventmethod D)
{
    D();
}

我想做的是代替在那儿调用D(),我想将该方法(D)存储为KeyEvent的成员变量,所以类似:

What I want to do is instead of calling D() there, I want to store that method (D) as a member variable of KeyEvent, so something like:

stored_method = D();

然后在另一个KeyEvent方法中,执行类似以下操作:

And then later in another method of KeyEvent, do something like:

stored_method();

我该怎么做?

推荐答案

您几乎已经有了代码.只需创建正确的委托类型的成员字段,然后将参数保存到该字段即可,就像处理其他任何数据类型一样.

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

这篇关于将方法存储为类的成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:43