Im trying to call a function from a c# dll via powershell, it require an object array as parameter and i need to pass strings inside it, but i dont know how.
What i need to do:
C# version:
printCustom(new object[] {new string[] {"hello", "its", "working"}});
I need to call this function from powershell but how to pass the parameters?
printCustom([object[]]@(//now?//));
thanks.
CodePudding user response:
Use the unary array operator ,
to wrap an enumerable type in an array - this will prevent PowerShell from unraveling the string-array when you then construct the array that'll actually get passed to the method:
[TargetType]::printCustom(@(,'hello its working'.Split()))
Let's test it:
# Generate test function that takes an array and expects it to contain string arrays
Add-Type @'
using System;
public class TestPrintCustom
{
public static void printCustom(object[] args)
{
foreach(var arg in args){
foreach(string s in (string[])arg){
Console.WriteLine(s);
}
}
}
}
'@
[TestPrintCustom]::printCustom(@(,"hello its working".Split()))
Which, as expected, prints:
hello
its
working