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

时间:2016-01-29 14:02:07

标签: powershell powershell-v2.0 powershell-v3.0 powermock

$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:\Powercel\PowershellPractice.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:\Powercel\PowershellPractice.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:\Powercel\PowershellPractice.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:\Powercel\PowershellPractice.ps1:155 char:19
+                         $minTime.Add <<<< ($threadInitDelay)
    + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

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

PS M:\Scripts\Inno\Output> [int[]]$sumlist = 5,5

PS M:\Scripts\Inno\Output> $sumlist.GetType().Fullname
System.Int32[]

PS M:\Scripts\Inno\Output> $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:\Scripts\Inno\Output> $sumlist = New-Object System.Collections.Generic.List[int]

虽然我没有得到与你相同的错误,但即使我尝试重新改编,变量的类型也没有改变。那是因为变量explained in this answer by PetSerAl的属性。查看命令Get-Variable sumlist | ft Name,Attributes应该查看System.Management.Automation.ArgumentTypeConverterAttribute。删除变量并重新开始我得到了您预期的行为。

PS M:\Scripts\Inno\Output> Remove-Variable sumlist

PS M:\Scripts\Inno\Output> $sumlist = New-Object System.Collections.Generic.List[int]

PS M:\Scripts\Inno\Output> $sumlist.Add(5)

PS M:\Scripts\Inno\Output> $sumlist.GetType().FullName
System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]