在powershell的List[int]中添加元素时显示错误


$sumList= New-Object System.Collections.Generic.List[int]
$eventList = New-Object System.Collections.Generic.List[string]
$preSumList= New-Object System.Collections.Generic.List[int]
$threadInitDelay = [int]$commaSplit[++$eventIndex].Split(':')[1].Trim()  
$preProcessTime = [int]$commaSplit[++$eventIndex].Split(':')[1].Trim()+[int]$commaSplit[++$eventIndex].Split(':')[1].Trim()+$commaSplit[[int]++$eventIndex].Split(':')[1].Trim()
$respTime = [int]$commaSplit[++$eventIndex].Split(':')[1].Trim();
$index =$eventList.FindIndex({param([string]$s) return $s -like 'hi'})
if($index -eq -1){
    $eventList.Add($eventName)
    $sumList.Add($threadInitDelay)
    $respSumList.Add($respTime)

收到此错误:

Method invocation failed because [System.Int32[]] doesn't contain a method named 'Add'.
At D:PowercelPowershellPractice.ps1:151 char:19
+                         $sumList.Add <<<< ($threadInitDelay)
    + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [System.Int32[]] doesn't contain a method named 'Add'.
At D:PowercelPowershellPractice.ps1:152 char:23
+                         $respSumList.Add <<<< ($respTime)
    + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.Int32[]] doesn't contain a method named 'Add'.
At D:PowercelPowershellPractice.ps1:153 char:23
+                         $respMaxTime.Add <<<< ($respTime)
    + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.Int32[]] doesn't contain a method named 'Add'.
At D:PowercelPowershellPractice.ps1:155 char:19
+                         $minTime.Add <<<< ($threadInitDelay)
    + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

你能帮帮我吗?

一种可能的情况是,您正在测试并实际转换了这些变量,例如错误状态。错误没有错。这些变量不是代码中的列表类型。我几乎可以重现这个问题:

PS M:ScriptsInnoOutput> [int[]]$sumlist = 5,5
PS M:ScriptsInnoOutput> $sumlist.GetType().Fullname
System.Int32[]
PS M:ScriptsInnoOutput> $sumlist.Add(5)
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At line:1 char:1
+ $sumlist.Add(5)
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotSupportedException
PS M:ScriptsInnoOutput> $sumlist = New-Object System.Collections.Generic.List[int]

虽然我没有收到与您相同的错误,但关键是即使我尝试重新转换变量,变量的类型也没有改变。这是因为PetSerAl在此答案中解释的变量的属性。查看命令Get-Variable sumlist | ft Name,Attributes您应该看到System.Management.Automation.ArgumentTypeConverterAttribute 。删除变量并重新开始,我得到了您的预期行为。

PS M:ScriptsInnoOutput> Remove-Variable sumlist
PS M:ScriptsInnoOutput> $sumlist = New-Object System.Collections.Generic.List[int]
PS M:ScriptsInnoOutput> $sumlist.Add(5)
PS M:ScriptsInnoOutput> $sumlist.GetType().FullName
System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

相关内容

  • 没有找到相关文章

最新更新