2016-01-29 30 views
1
$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) 

收到此错误:显示错误,而在中的powershell列表添加元素[INT]?

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 

能否请你帮我吗?

+0

什么是那些变量的类型?我的意思是用'$ sumList.GetType()。FullName'来检查它。我可以看到你的代码,但如果你有一个开放的会话,这些变量被转换成不同的方式,他们不会工作。重新启动会话或打开新的控制台并再次测试以确保安全。 Mathis在另一个问题中建议你使用'remove-variable'。 '$ sumList =新物体System.Collections.Generic.List [INT]; $ sumList.Add(5)'为我工作。错误中的类型看起来不正确。 – Matt

+0

这可能是一个愚蠢的目标。 http://stackoverflow.com/questions/27830659/same-input-into-mathround-returns-different-results/27873156#27873156。如果我对演员问题是正确的,那么答案就是解释问题。 – Matt

+0

感谢man..got答案 –

回答

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] 

虽然我没有得到同样的错误,你点的是变量的类型,即使我试图recasted它并没有改变。这是因为变量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]] 
+0

得到它现在..its工作 –