首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在powershell中在List[int]中添加元素时显示错误?

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

Stack Overflow用户
提问于 2016-01-29 14:02:06
回答 1查看 672关注 0票数 1
代码语言:javascript
复制
$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)

获取此错误:

代码语言:javascript
复制
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

你能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-29 14:50:31

一种可能的情况是,您正在测试并实际上抛出了这些变量,比如错误状态。这个错误不是错的。这些变量不是代码中的列表类型。我几乎可以复制这个问题:

代码语言:javascript
复制
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。移除变量并重新启动,我将得到您预期的行为。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35086417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档