我正在尝试在 Powershell 中创建字典并将其传递给 C# 函数
$params= @{"abc"="123","xyz"=$False};
C# 方法接受 Dictionary<string,object>
作为参数。
当我尝试调用MyFunc($params)
时从 PowerShell 中,我收到以下错误
Cannot convert argument "arguments", with value: "System.Collections.Hashtable", for "MyFunc" to type "System.Collections.Generic.Dictionary2[System.String,System.String]": "Cannot create object of type "System.Collections.Generic.Dictionary2[System.String,System.String]"
最佳答案
正如它所说,您已在 PowerShell 中创建了 HashTable
,但尝试在 C# 中将其用作 Dictionary
。你必须改变一切,要么是其中一种,要么是另一种。以防万一,以下是 Powershell 的字典声明:
$params = New-Object 'System.Collections.Generic.Dictionary[String,object]'
$params.Add("abc","123")
或者您可以更改 C# 方法以接受 HashTable
关于c# - 无法将 "System.Collections.Hashtable"转换为类型“System.Collections.Generic.Dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40084485/