I am implementing a DynamicObject
. In the TryInvokeMethod
, in addition to the arguments ( passed in to the method), I also need the names for the parameters if they have been used.
I can see that binder.CallInfo.ArgumentNames
does indeed provides the names, but I am not able to associate them with the values. Is there any way to do so, or am I hoping against hope:
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
var names = binder.CallInfo.ArgumentNames;
foreach (var arg in args)
{
arguments.Add(new Argument(arg));
}
//I need to associate the names and the args
result = this;
return true;
}
So for example if I do a call like:
myDynamicObject.DynamicInvoke("test", something: "test2")
I have test
and test2
, and also something
, but I am not able to get the information that something
was the name for test2
and test
had no name.
I had to use the fact that named arguments occur only after the non-named ones are specified ( thanks to user629926 for pointing to the obvious) and initial code:
var names = binder.CallInfo.ArgumentNames;
var numberOfArguments = binder.CallInfo.ArgumentCount;
var numberOfNames = names.Count;
var allNames = Enumerable.Repeat<string>(null, numberOfArguments - numberOfNames).Concat(names);
arguments.AddRange(allNames.Zip(args, (flag, value) => new Argument(flag, value)));