Reputation: 11
I currently have parameters as an object array, but I would like to pass each individual key into a different function instead of passing the array. Here is a code example:
FunctionOne("variableOne", "variableTwo");
public void FunctionOne(params object[] args) {
FunctionTwo(args);
}
Upvotes: 1
Views: 179
Reputation: 116
If you want it to work with the arguments array of dynamic size you can use Reflection to achieve this
typeof(YourClassName).GetMethod(nameof(FunctionTwo)).Invoke(this, args);
But be aware that this kind of operations are much slower than calling method directly. You can also save MethodInfo
in a static variable to call .GetMethod() only once like so:
private static MethodInfo FunctionTwoMethodInfo = typeof(YourClassName).GetMethod(nameof(FunctionTwo));
And than call it like so:
FunctionTwoMethodInfo.Invoke(this, args);
Upvotes: 0
Reputation: 4283
Just grab the array argument you want:
FunctionOne("variableOne", "variableTwo");
public void FunctionOne(params object[] args) {
FunctionTwo(args[0]);
}
So if you want every single argument, you can iterate with a foreach loop:
FunctionOne("variableOne", "variableTwo");
public void FunctionOne(params object[] args)
{
foreach (object item in args)
{
FunctionTwo(item);
}
}
Upvotes: 2