我试图将输出强制Get-Content myfile.txt
转换为ArrayList
对象,以便可以轻松地使用.add
and 插入和更改字符串.insert
。我试过的是
[Systems.Collections.ArrayList]mylist=@() Get-Content myfile | $mylist # obviously wrong Get-Content myfile | ForEach-Object {$mylist} # don't quite grasp the logic, get empty array as a result Get-Content myfile | ForEach-Object {$mylist.Add()} # get overload error
如果我只分配$mylist=Get-Content myfile.txt
它,它将数据类型更改为我不想要的静态数组
Get-Content返回一个数组,因此您可以直接将其强制转换为ArrayList:
$mylist = [System.Collections.ArrayList](Get-Content myfile) $mylist.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False ArrayList System.Object
现在,您可以使用.add()
或.insert()
方法进行修改,例如:
$mylist.insert(2,"new content")