Func<> with unknown number of parameters

前端 未结 6 1554
独厮守ぢ
独厮守ぢ 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:20

    This could become easy with lambda expressions:

    TResult Foo(Func f)
    {
      TResult result = f();
      return result;
    }
    

    Then usage coul be like:

    var result = Foo(() => method(arg1, arg2, arg3));
    

    Where method can be arbitrary method returning int.

    This way you can pass any number of any erguments directly through lambda.

提交回复
热议问题