简体   繁体   English

如何将一个方法设置为另一个方法的参数

[英]How to set a method to the parameter of another method

I need to set a method into the parameter of another function. 我需要将一个方法设置为另一个函数的参数。 So it should look something like this (just pseudocode): 所以它应该看起来像这样(只是伪代码):

void toDoAnotherMethod(anotherMethod()) {
     anotherMethod();
}

void justMethod() {
     *some stuff to do*
}

void Start() {
    toDoAnotherMethod(justMethod);
}

But I totally don't understand how to do this thing in real code. 但是我完全不了解如何在真实代码中执行此操作。 Can somebody help me? 有人可以帮我吗?

Thanks. 谢谢。

Use Action : 使用Action

void toDoAnotherMethod(Action anotherMethod)
{
    anotherMethod();
}

void justMethod()
{
//            *some stuff to do *
}

void Start()
{
    toDoAnotherMethod(justMethod);
}

Also, if your methods have parameters, you can use Action< T1, …> , and if they return a value, you should use Func<TResult>, Func<T1, TResult> etc. 另外,如果您的方法具有参数,则可以使用Action< T1, …> ,并且如果它们返回值,则应使用Func<TResult>, Func<T1, TResult>等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM