我正在使用Powershell 5並嘗試在PowerShell中調用某些.net類方法。在這裏,我下面的例子:https://msdn.microsoft.com/en-us/library/d9hy2xwa(v=vs.110).aspx[System.Array] ::查找方法在powershell中不受支持
然後寫在PowerShell中的一些代碼:
$arr = @()
$arr += 10
$arr += 5
$arr += 7
[System.Array]::find($arr, x => x -eq 10)
但我得到的錯誤:
> At line:1 char:21
> + [array]::find($arr, x => x -eq 5)
> + ~ Missing expression after ','. At line:1 char:22
> + [array]::find($arr, x => x -eq 5)
> + ~ Unexpected token 'x' in expression or statement. At line:1 char:35
> + [array]::find($arr, x => x -eq 5)
> + ~ Unexpected token ')' in expression or statement.
> + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
> + FullyQualifiedErrorId : MissingExpressionAfterToken
'[ Array] :: Find((1..10),[Predicate [Object]] {$ args [0] -eq 5})' – PetSerAl
[Array.Find on powershell array](http:// stackoverflow。 com/questions/21209946/array-find-on-powershell-array) – wOxxOm
應該注意的是** [Array] :: Find是〜50倍比直接比較'** arr -eq 10 |更慢** select -first 1'當然,你可能沒有注意到在數組上使用'+ ='失去時間和速度以及內存之後(PS每次創建一個新數組!!使用ArrayList或System.Collections.Generic。*類型代替) – wOxxOm