fooName
私の目標は、2 つの離散変数 (とfooUrl
) と のリストを持つカスタム データ オブジェクトを作成することですfooChildren
。各リスト項目には 2 つの離散変数 variablechildAge
とがありchildName
ます。
現在、私はこれを持っています:
$fooCollection = [PSCustomObject] @{fooName=""; fooUrl=""; fooChildrenList=@()}
$fooCollection.fooName = "foo-a-rama"
$fooCollection.fooUrl = "https://1.2.3.4"
$fooChild = New-Object -TypeName PSobject
$fooChild | Add-Member -Name childAge -MemberType NoteProperty -Value 6
$fooChild | Add-Member -Name childName -MemberType NoteProperty -Value "Betsy"
$fooCollection.fooChildrenList += $fooChild
$fooChild = New-Object -TypeName PSobject
$fooChild | Add-Member -Name childAge -MemberType NoteProperty -Value 10
$fooChild | Add-Member -Name childName -MemberType NoteProperty -Value "Rolf"
$fooCollection.fooChildrenList += $fooChild
cls
$fooCollection.fooName
$fooCollection.fooUrl
foreach ($fooChild in $fooCollection.fooChildrenList)
{
(" " + $fooChild.childName + " " + $fooChild.childAge)
}
これにより、以下が生成されます。ここまでは順調ですね
foo-a-rama
https://1.2.3.4
Betsy 6
Rolf 10
問題: 私が+=
理解しているように、使用すると、実行されるたびに (どのような状態でも)+=
のコピーが作成されるため、使用するのは好きではありません。$fooCollection.fooChildrenList
+=
fooChildrenList
したがって、 asを実装する代わりに、必要に応じて各行を追加できるようにas@()
を実装したいと思います。コードでこれを行うさまざまな方法を試しましたが、最終的には人口が不足しています。例えば:fooChildrenList
New-Object System.Collections.ArrayList
fooChildrenList
$fooCollection = [PSCustomObject] @{fooName=""; fooUrl=""; fooChildrenList = New-Object System.Collections.ArrayList}
$fooCollection.fooName = "foo-a-rama"
$fooCollection.fooUrl = "https://1.2.3.4"
$fooChild.childName = "Betsy"
$fooChild.childAge = 6
$fooCollection.fooChildrenList.Add((New-Object PSObject -Property $fooChild))
$fooChild.childName = "Rolf"
$fooChild.childAge = 10
$fooCollection.fooChildrenList.Add((New-Object PSObject -Property $fooChild))
$fooCollection | get-member
ショー
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
fooChildrenList NoteProperty System.Collections.ArrayList fooChildrenList=
fooName NoteProperty string fooName=foo-a-rama
fooUrl NoteProperty string fooUrl=https://1.2.3.4
$fooCollection
ショー
fooName : foo-a-rama
fooUrl : https://1.2.3.4
fooChildrenList : {}
System.Collections.ArrayList を PowerShell カスタム オブジェクトに追加するにはどうすればよいですか?