我需要将一个方法设置为另一个函数的参数。所以它应该看起来像这样(只是伪代码):
void toDoAnotherMethod(anotherMethod()) {
anotherMethod();
}
void justMethod() {
*some stuff to do*
}
void Start() {
toDoAnotherMethod(justMethod);
}
但我完全不明白如何在实际代码中做这件事。有人可以帮助我吗?
谢谢。
使用Action
:
void toDoAnotherMethod(Action anotherMethod)
{
anotherMethod();
}
void justMethod()
{
// *some stuff to do *
}
void Start()
{
toDoAnotherMethod(justMethod);
}
此外,如果您的方法有参数,您可以使用Action< T1, …>
,如果它们返回一个值,您应该使用Func<TResult>, Func<T1, TResult>
等。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句