string array inside object array in Powershell

root823746374

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.

Mathias R. Jessen

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章