c# - 无法将 "System.Collections.Hashtable"转换为类型“System.Collections.Generic.Dictionary

标签 c# powershell

我正在尝试在 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/

相关文章:

c# - 如何在 Xamarin.Forms 中使用 ReactiveUI 只执行一次命令?

c# - 写入文件后立即读取文件我得到全零(.net)

c# - 如何检查Windows用户是否设置了密码?

powershell - 如何在管道中处理$ null

c# - Request.IsAjaxRequest 不工作 mvc

c# - 有没有办法在 C# 中简化这个反射委托(delegate)代码?

powershell - 不确定Get-ChildItem结果格式

powershell - 批处理文件未正确读取标志

rest - Powershell正在吞噬REST错误响应

powershell - 能否将一个变量拆分为多个变量?