C# 无法转换";System.Collections.Hashtable“;输入“;System.Collections.Generic.Dictionary
我正在尝试在Powershell中创建字典,并将其传递给C#函数 C#方法接受C# 无法转换";System.Collections.Hashtable“;输入“;System.Collections.Generic.Dictionary,c#,powershell,C#,Powershell,我正在尝试在Powershell中创建字典,并将其传递给C#函数 C#方法接受字典作为参数 当我尝试从PowerShell调用MyFunc($params)时,我发现以下错误 无法转换参数“arguments”,其值为: “System.Collections.Hashtable”,用于键入“MyFunc” “System.Collections.Generic.Dictionary2[System.String,System.String]”: “无法创建类型为的对象 System.Colle
字典
作为参数
当我尝试从PowerShell调用MyFunc($params)
时,我发现以下错误
无法转换参数“arguments”,其值为:
“System.Collections.Hashtable”,用于键入“MyFunc”
“System.Collections.Generic.Dictionary2[System.String,System.String]”:
“无法创建类型为的对象
System.Collections.Generic.Dictionary2[System.String,System.String]
它是这样说的,您已经在PowerShell中创建了
哈希表
,但试图将其用作C#中的字典。您必须将所有内容更改为一个或另一个。以防万一,下面是PowerShell的字典声明:
$params = New-Object 'System.Collections.Generic.Dictionary[String,object]'
$params.Add("abc","123")
或者您可以将C#方法更改为接受哈希表
C#接受字符串、对象类型。我确实尝试了此方法$params=New object System.Collections.Generic.Dictionary[string,object]$params.Add(“abc”、“123”),但我遇到了此错误,无法转换'System.object[]参数“ComObject”所需的“System.String”类型。不支持指定的方法。
对我来说效果很好(如果您获得正确的引号)。你的powershell版本是什么?这很奇怪。没有quote'
its'不起作用。但是我已经习惯了很长一段时间创建没有引号的对象newobject com.abc。test
在其他情况下工作正常
$params = New-Object 'System.Collections.Generic.Dictionary[String,object]'
$params.Add("abc","123")