2016-12-05 62 views
0

我需要在PowerShell中實例化IDictionary對象,但無法弄清楚正確的語法是什麼。如何在Powershell中實例化IDictionary?

Import-module docker 
$config = [Docker.DotNet.Models.Config]::new() 
$dict = new-object 'system.collections.generic.dictionary[string,string]' 
$dict.Add("d:\container\content", "c:\inetpub\wwwrooot\content") 
$config.Volumes = $dict 

Exception setting "Volumes": "Cannot convert the "System.Collections.Generic.Dictionary`2[System.String,System.String]" value of type "System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" to type "System.Collections.Generic.IDictionary`2[System.String,System.Object]"." 
At D:\docker\run-MFEcountainer.ps1:6 char:1 
+ $config.Volumes = $dict 
+ ~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], SetValueInvocationException 
    + FullyQualifiedErrorId : ExceptionWhenSetting 

回答

1

您的錯誤消息告訴您,它期望與您傳遞的不同類型。

看起來你應該這樣做:

$dict = new-object 'system.collections.generic.dictionary[string,object]'