PowerShell类;不动产;“系统.集合.专门化.定购词典”;;如何添加字典条目

PowerShell类;不动产;“系统.集合.专门化.定购词典”;;如何添加字典条目,powershell,class,dictionary,add,Powershell,Class,Dictionary,Add,(由于我已经在网上搜索了相当多的答案,但没有得到任何正确方向的提示,我允许自己问你以下问题:) 我有这样一个PowerShell类: class settings { [string]$CFG_Name [string]$CFG_Template_Path [string]$CFG_Target_Path [System.Collections.Specialized.OrderedDictionary]$ConfigSettings } $CurrentSet

(由于我已经在网上搜索了相当多的答案,但没有得到任何正确方向的提示,我允许自己问你以下问题:)

我有这样一个PowerShell类:

class settings
{
    [string]$CFG_Name
    [string]$CFG_Template_Path
    [string]$CFG_Target_Path
    [System.Collections.Specialized.OrderedDictionary]$ConfigSettings
}
$CurrentSettings = New-Object settings    <-- works fine
$CurrentSettings.CFG_Name = 'MPD'    <-- works fine
$CurrentSettings.CFG_Template_Path = $CFG_Template_MPD_Path    <-- works fine
$CurrentSettings.CFG_Target_Path = $CFG_MPD_Path    <-- works fine

$CurrentSettings.ConfigSettings.Add("TID",$EFT_TID)    <-- doesn't work
$CurrentSettings.ConfigSettings = New-Object System.Collections.Specialized.OrderedDictionary
然后我实例化这个类,并希望像这样使用它:

class settings
{
    [string]$CFG_Name
    [string]$CFG_Template_Path
    [string]$CFG_Target_Path
    [System.Collections.Specialized.OrderedDictionary]$ConfigSettings
}
$CurrentSettings = New-Object settings    <-- works fine
$CurrentSettings.CFG_Name = 'MPD'    <-- works fine
$CurrentSettings.CFG_Template_Path = $CFG_Template_MPD_Path    <-- works fine
$CurrentSettings.CFG_Target_Path = $CFG_MPD_Path    <-- works fine

$CurrentSettings.ConfigSettings.Add("TID",$EFT_TID)    <-- doesn't work
$CurrentSettings.ConfigSettings = New-Object System.Collections.Specialized.OrderedDictionary

$CurrentSettings=新对象设置解决方案如下:

在添加第一个字典条目之前,必须按如下方式初始化属性:

class settings
{
    [string]$CFG_Name
    [string]$CFG_Template_Path
    [string]$CFG_Target_Path
    [System.Collections.Specialized.OrderedDictionary]$ConfigSettings
}
$CurrentSettings = New-Object settings    <-- works fine
$CurrentSettings.CFG_Name = 'MPD'    <-- works fine
$CurrentSettings.CFG_Template_Path = $CFG_Template_MPD_Path    <-- works fine
$CurrentSettings.CFG_Target_Path = $CFG_MPD_Path    <-- works fine

$CurrentSettings.ConfigSettings.Add("TID",$EFT_TID)    <-- doesn't work
$CurrentSettings.ConfigSettings = New-Object System.Collections.Specialized.OrderedDictionary
然后可以添加条目:

$CurrentSettings.ConfigSettings.Add("TID",$EFT_TID)

你也可以做
.ConfigSettings=[ordered]@{TID=$EFT\u TID}
重复我看不出世界上哪里应该重复我的问题。与此无关。