如何将调用传递给函数?

Eug*_*ene 0 .net visual-studio-2010 c#-4.0

我需要将一个函数传递给另一个函数并在那里调用它.我使用以下代码:

namespace DelegateTest
{
    class Program
    {
        public class MySampleClass
        {
            public void click(string param)
            {
                System.Console.WriteLine(param + " click!");
            }

            public void flick(string param)
            {
                System.Console.WriteLine(param + " flick!");
            }
        }

        public static delegate void EventToWait(string param);

        public static void waitFor(EventToWait myEvent, string param)
        {
            // here is a **very** complex loop, which i would like wipe from main() function
            myEvent(param);
        }

        public static void Main(string[] args)
        {
            MySampleClass sc = new MySampleClass();
            EventToWait ev1 = sc.click;
            EventToWait ev2 = sc.flick;

            waitFor(ev1, "Button_1");
            waitFor(ev2, "Button_2");

            System.Console.ReadKey();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我的编译器说,该委托不能是静态的.我怎么能处理这个问题?

Dar*_*rov 5

好吧,只需删除static关键字:

public delegate void EventToWait(string param);
Run Code Online (Sandbox Code Playgroud)