我需要在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
答案 0 :(得分:1)
您的错误消息告诉您它所期望的类型与您传递的类型不同。
看起来你应该这样做:
$dict = new-object 'system.collections.generic.dictionary[string,object]'