Variable parameters in C# Lambda

前端 未结 5 1282
别跟我提以往
别跟我提以往 2021-01-01 23:07

Is it possible to have a C# lambda/delegate that can take a variable number of parameters that can be invoked with a Dynamic-invoke?

All my attempts to use the \'par

5条回答
  •  一向
    一向 (楼主)
    2021-01-01 23:25

    Adding to Mark's answer, I'd create an extension method to clean up a bit:

    static DynamicInvokeParams(this ParamsDelegate delegate, params object[] args)
    {
      delegate.DynamicInvoke(new [] {args});
    }
    

    And then you just have to say:

    paramsDelegate.DyanamicInvokeParams(1, 2, 3);
    

提交回复
热议问题