Func<> with unknown number of parameters

前端 未结 6 1542
独厮守ぢ
独厮守ぢ 2020-12-06 00:37

Consider the following pseudo code:

TResult Foo(Func f, params object[] args)
{
    TResult result = f(args);
           


        
6条回答
  •  独厮守ぢ
    2020-12-06 01:22

    That's not possible. At best, you could have a delegate that also takes a variable number of arguments, and then have the delegate parse the arguments

    TResult Foo(Func f, params object[] args)
    {
        TResult result = f(args);
        return result;
    }
    


    Foo(args =>
    {
        var name = args[0] as string;
        var age = (int) args[1];
    
        //...
    
        return age;
    }, arg1, arg2, arg3);
    

提交回复
热议问题