我正在尝试在 powershell 中创建一组自定义对象并将它们存储在哈希表中。问题是当我将对象放入哈希表时自定义属性消失了。
$customObject = New-Object object
$customObject | Add-member -membertype noteproperty -name customValue -value "test"
Write-Host $customObject.customValue
$hashTable = @{}
$hashTable.add("custom", $customObject)
$object = $hashTable["custom"]
$object.customValue = 7
当我执行此代码时,我得到以下输出。
test
Property 'customValue' cannot be found on this object; make sure it exists and is settable.
At C:\temp\test2.ps1:15 char:9
+ $object. <<<< customValue = 7
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
将自定义属性放入集合后,有什么方法可以使用它吗?