想法1:
比如说,使用泛型来定义该函数a?然后写多个函数重载?
例子:
//传入函数Func<T1,T2,TResult> func,等待time秒后执行它
public static IEnumerator WaitForExecute(float time,Func<T1,T2,TResult> func)
    {
        yield return new WaitForSeconds(time);
       //这里执行传入的函数,我不会写
    }想法2:
通过反射,获取传入函数的参数类型。用MethodInfo和Invoke来做。
//初学,还不会写
想看看前辈的做法!

解决方案 »

  1.   

    Func<T1,T2,TResult> func的话,还要传入参数,即具体的T1和T2;以及返回值TResult的去向如何。
      

  2.   

    linq中有很多传入函数的例子。比如Where(Predicate p)中Predicate就是一个函数。
      

  3.   

    用委托回调:
    给你举个例子,代码是VB.net写的你自己转一下C#'新建一个类,定义委托
    Public Class Delegates
        Public Delegate Function Callback(Of In T1, In T2, Out TResult)(a As T1, b As T2) As TResult
    End Class
    Public Class Test
        Private Function b(arg1 As Integer, arg2 As Integer) As Integer
                  'doevents
        End Function
        Public Sub a(time as Single,callBack As Delegates.Callback(Of Integer, Integer, Integer), t1 As Integer, t2 As Integer)
            yield return new WaitForSeconds(time);
            callBack.Invoke(t1, t2)
        End Sub
        
        Sub c()
            '调用
            a(1000,New Delegates.Callback(Of Integer, Integer, Integer)(AddressOf f), 0, 0)
        End Sub
    End Class
      

  4.   

    !!!谢谢,我明白了!写了些小例子!
    ————————————————————————————————
    方法1:
    例子1:
    public static IEnumerator WaitForExecute<T1,T2>(float time, Action<T1, T2> action,T1 t1,T2 t2)
        {
            yield return new WaitForSeconds(time);
           action (t1, t2);
        }
      private void Add(int a,int b)
        {
            int c = a + b;
            //Console.WriteLine(c);
            Debug.Log(c);
        }
     private void OnContinueButtonClick()
        {
            StartCoroutine(WaitForExecute<int, int>(3f, Add, 1, 2));

    结果:停顿3秒后才执行。例子2:
    //实验函数名和实际功能无关
    public static void WaitForExecute<T1,T2>(float time, Action<T1, T2> action,T1 t1,T2 t2)
        {
           action (t1, t2);
        }
        public static TResult WaitForExecute<T1, T2,TResult>(float time,  Func<T1, T2,TResult> func, T1 t1, T2 t2,TResult result)
        {
            result = func(t1, t2);
            return result;
        }
        public static void WaitForExecute<T1, T2, TResult>(float time, Func<T1, T2, TResult> func, T1 t1, T2 t2, out TResult result)
        {
            result = func(t1, t2);
        }
        private void Add(int a,int b)
        {
            int c = a + b;
            //Console.WriteLine(c);
            Debug.Log("c:"+c);
        }
        private int GetAdd(int a,int b)
        {
            return a + b;
        }
        private void OnContinueButtonClick()
        {
           WaitForExecute<int, int>(3f, Add, 1, 2);
          int a= WaitForExecute<int, int,int>(3f, GetAdd, 3, 2, score);
            Debug.Log("a:" + a);
            Debug.Log("score1:" + score);
            WaitForExecute<int, int, int>(3f, GetAdd, 3, 2, out score);
            Debug.Log("score1:"+score);

    结果:
    ——————————————————————————————————————————————————————
    方法2:
    不怎么会,有机会补充,有大佬可以帮忙补充……
      

  5.   

    已经被xx园给带入邪路,记住永远不存在万能方法,万能函数。
    如果哪一天无论那个园出现了万能方法,万能对象,万能函数----------那个人不是骗子,就是神。
    如果他是神-----------恭喜你,你可以解脱苦海了,这世界将不存在程序员这个行业了,因为他已经万能了,我说要有光,他就有光了,还需要程序员干啥为啥这么说
    1.Func<T1,T2,TResult> func,你的前提是有T1,T2,如果是万能的,你需要管T1,T2么
    2.外面的调用的人才知道要干啥,我写这个玩意的时候根本就不知道你要干啥。你写的N个例子,前提就根本不存在,你并没有做什么把任意函数b传入a,你只是传入了我所期望的Func<T1,T2,TResult>,无论你那个例子写了多少N种函数,其实都是一个Func<T1,T2,TResult>,他既不伟大,也不万能,他是我规定的你就必须这样传入
      

  6.   

    LINQ可以以方法名为参数调用