【问题标题】:PowerShell Class; property "System.Collections.Specialized.OrderedDictionary"; how to add dictionary entryPowerShell 类;属性“System.Collections.Specialized.OrderedDictionary”;如何添加字典条目
【发布时间】:2018-07-06 16:07:59
【问题描述】:

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

我有一个这样的 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

上面的行抛出:您不能在空值表达式上调用方法。

根据这个页面:Documentation from Microsoft支持add方法。

为什么我不能像使用 $CFG_Name 一样使用这个属性?

如何将字典条目添加到此属性?

【问题讨论】:

标签: powershell class dictionary add


【解决方案1】:

解决方法如下:

在添加第一个字典条目之前,您必须像这样初始化属性:

$CurrentSettings.ConfigSettings = New-Object System.Collections.Specialized.OrderedDictionary

然后你可以添加条目:

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

【讨论】:

  • 你也可以.ConfigSettings = [ordered]@{ TID = $EFT_TID }
猜你喜欢
  • 2011-02-07
  • 1970-01-01
  • 2020-10-05
  • 2020-05-04
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
相关资源
最近更新 更多