Can named arguments be used with params modifier?

I am trying to call Invokeon Dispatcherusing the following overload:

public object Invoke(Delegate method, params object[] args);

I want to use named arguments, but I cannot find the argument syntax with the params modifier. All of the following will not compile:

dispatcher.Invoke(method: () => { }, args: {});
dispatcher.Invoke(method: () => { }, args: new object[0]);
dispatcher.Invoke(method: () => { }, args: null);
dispatcher.Invoke(method: () => { }, args: new object[] {});
object[] foo = {};
dispatcher.Invoke(method: () => { }, args: foo);
dispatcher.Invoke(method: () => { }, args: new[] {"Hello", "World!"});

I found these two questions, which seem to have no definite answers:

Named parameters with parameters

How to set a named argument to string.Format?

So my question is: can this be done or not? If so, how?

UDPATE

Daniel Hilgart shows that yes paramscan be used with named parameters. I included his answer using this template:

Action method = () => { };
if (_dispatcher != null)
    _dispatcher.Invoke(method: method, args: null);
else
    method();
+5
source share
2 answers

:

void Main()
{
    Invoke(method: () => {}, args: new object[] {});
}

public object Invoke(Action method, params object[] args)
{
    return null;
}

Delegate Action, , () => {} Delegate.

, :

void Main()
{
    Action method = () => {};
    Invoke(method: method, args: new object[] {});
}

public object Invoke(Delegate method, params object[] args)
{
    return null;
}

, args, method, : " " - "" System.Delegate ". lambda Action (Invoke(method: (Action)(() => {}) ...), Action, (. ), Action Delegate.

+2

:

dispatcher.Invoke(method: () => { }, args: new[] {"Hello", "World!"});
+1

All Articles