无法将“System.Collections.Hashtable”转换为类型“System.Collections.Generic.Dictionary”

Red*_*ddy 2 c# powershell

我正在尝试在 Powershell 中创建字典并将其传递给 C# 函数

$params= @{"abc"="123","xyz"=$False};
Run Code Online (Sandbox Code Playgroud)

C# 方法接受Dictionary<string,object>作为参数。

当我尝试MyFunc($params)从 PowerShell调用时,出现以下错误

无法转换参数“arguments”,值为“System.Collections.Hashtable”,“MyFunc”输入“System.Collections.Generic.Dictionary2[System.String,System.String]”:“无法创建类型的对象” System.Collections.Generic.Dictionary2[System.String,System.String]"

And*_*huk 5

这就是它所说的,您已HashTable在 PowerShell 中创建,但尝试将其用作DictionaryC# 中的 a。你必须改变一切,成为一个或另一个。以防万一,这里是 Powershell 的字典声明:

$params = New-Object 'System.Collections.Generic.Dictionary[String,object]'
$params.Add("abc","123")
Run Code Online (Sandbox Code Playgroud)

或者你可以改变你的 C# 方法来接受 HashTable