Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用params关键字从PowerShell调用C#函数_C#_.net_Powershell - Fatal编程技术网

使用params关键字从PowerShell调用C#函数

使用params关键字从PowerShell调用C#函数,c#,.net,powershell,C#,.net,Powershell,我通过以下方式从PowerShell调用C#函数: // C# code public void Foo(string param1, int param2, Dictionary<string, string> param3) # PowerShell code: Add-Type -Path $pathToDll $myClass= New-Object -TypeName MyClass $dic= New-Object 'System.Collections.Gene

我通过以下方式从PowerShell调用C#函数:

// C# code
public void Foo(string param1, int param2, Dictionary<string, string> param3)

# PowerShell code:  
Add-Type -Path $pathToDll

$myClass= New-Object -TypeName MyClass
$dic= New-Object 'System.Collections.Generic.Dictionary[String,String]'
$dic.Add("Key1","Val1")
$dic.Add("Key2","Val2")

$myClass.Foo("Test1", 1, $dic)
如何从powershell调用此函数

如果我这样称呼它:

 $myClass.Foo("Test1", 1, "foo1", "foo2", "foo3")
$myClass.Foo("Test1", 1, @("foo1", "foo2", "foo3"))

它显示了一个错误:找不到“Foo”的重载,参数计数为:“5”。

我认为您必须这样称呼它:

 $myClass.Foo("Test1", 1, "foo1", "foo2", "foo3")
$myClass.Foo("Test1", 1, @("foo1", "foo2", "foo3"))

基本上,您必须使用称为显式数组的
@()

为第三个参数创建一个数组。为了澄清Martin的答案,您的方法签名告诉您该方法包含3个参数,一个字符串、一个int和一个字符串数组。因此,您必须创建一个字符串数组,将其存储在变量中并将其作为第三个参数,或者使用Martin描述的方法(我更喜欢最后一个)