将方法存储为类的成员变量

发布于 2024-08-17 01:54:47 字数 387 浏览 6 评论 0原文

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

private delegate void eventmethod();

构造函数:

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

我想要做的是不在那里调用 D(),而是将该方法 (D) 存储为 KeyEvent 的成员变量,因此类似:

stored_method = D();

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

stored_method();

我怎样才能做到这一点?

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

private delegate void eventmethod();

And the constructor:

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

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

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

stored_method();

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不打扰别人 2024-08-24 01:54:47

您几乎已经有了代码。只需创建正确委托类型的成员字段并将参数保存到其中,就像使用任何其他数据类型一样。

private eventmethod MySavedEvent;

public void KeyEvent(eventmethod D) {
    // Save the delegate
    MySavedEvent = D;
}

public void CallSavedEvent() {
    if (MySavedEvent != null) {
        MySavedEvent();
    }
}

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();
    }
}
雨落星ぅ辰 2024-08-24 01:54:47

您可以执行以下操作:

private Action CallBackFunction {get; set;}

public KeyEvent(Action callback) {
   CallBackFunction = callback;
}

可以通过以下方式实例化:

new KeyEvent(MyFunction);

其中 MyFunction 是该类中某个函数的名称

KeyEvent 类可以使用以下方式调用该函数:

CallBackFunction();

Action 类还允许传入强类型参数,并且 Func 类可以用于返回结果的方法。

You could do something like:

private Action CallBackFunction {get; set;}

public KeyEvent(Action callback) {
   CallBackFunction = callback;
}

Which can be instantiated by:

new KeyEvent(MyFunction);

Where MyFunction is the name of some function in that class

The KeyEvent class can then invoke that function with:

CallBackFunction();

The Action class also allows strongly typed parameters to be passed in and the Func class can be used for methods that return a result.

夏夜暖风 2024-08-24 01:54:47

你想要的东西叫做“第一类函数” http://en.wikipedia.org/ wiki/First-class_function

C# 从版本 3 开始,支持匿名函数和 lambda 表达式。

返回 R 类型的值

因此,您可以使用 Func 表示一个函数,该函数采用 A 类型的参数并从 wiki

static Func<double, double> MakeDerivative(Func<double, double> f, double deltaX)
  {
    return (x) => (f(x + deltaX) - f(x - deltaX)) / (2 * deltaX);
  }

the thing that you want is called "first class function" http://en.wikipedia.org/wiki/First-class_function

C#, since version 3, supports anonymous functions and lambda expressions.

So you can use Func<A,R> which represents a function taking an argument of type A and returning a value of type R

from wiki

static Func<double, double> MakeDerivative(Func<double, double> f, double deltaX)
  {
    return (x) => (f(x + deltaX) - f(x - deltaX)) / (2 * deltaX);
  }
扬花落满肩 2024-08-24 01:54:47
public class KeyEvent
{
  private eventmethod hurr;
  public KeyEvent(eventmethod D)
  {
    hurr = D;
  }
  public void SomeOtherMethod()
  {
    hurr();
  }
}

一些事情...... PascalCase 类型/委托/等(EventMethod)和camelCase 参数(d,dIsMyMethod)。并重用框架为您提供的内容:private Action hurr;

public class KeyEvent
{
  private eventmethod hurr;
  public KeyEvent(eventmethod D)
  {
    hurr = D;
  }
  public void SomeOtherMethod()
  {
    hurr();
  }
}

Couple things... PascalCase types/delegates/etc (EventMethod) and camelCase arguments (d, dIsMyMethod). And reuse what the framework gives you: private Action hurr;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文