在 Windows 11 上使用 PowerShell ISE
PS C:\Users\malcolm> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.22000.282
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.22000.282
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
我需要在 PowerShell 中傳遞一個類屬性作為參考。
例如像這樣:
Function Add-Five
{
param([ref]$value)
$value.Value =5
}
$a = 5
Add-Five -value ([ref]$a)
Write-Host "A = $a"
正如預期的那樣,這輸出10
但是,如果我這樣做:
class MyClass
{
$a
}
Function Add-Five
{
param([ref]$value)
$value.Value =5
}
$class = New-Object MyClass
$class.a = 5
Add-Five -value ([ref]$class.a)
Write-Host "A = $($class.a)"
輸出為5。
作為一種解決方法,我可以這樣做:
Function Add-FiveSpecial
{
param($className,$propertyName)
(Get-Variable -Name $className).Value.$propertyName = 5
}
Add-FiveSpecial -className "class" -propertyName "a"
Write-Host "A = $($class.a)"
輸出10
有沒有辦法讓 [ref] 使用類屬性?
uj5u.com熱心網友回復:
類(它不是關鍵字)的主要目的[ref]是便于呼叫具有和引數的.NET API。refout
[ref]很少在純 PowerShell 代碼中使用,最好避免使用,因為它偏離了通常傳遞引數的方式,在語法上很麻煩,并且存在陷阱,例如手頭的陷阱。
簡而言之:
[ref]僅適用于 PowerShell變數,它真正為給定變數物件創建別名,因此無論您使用原始名稱還是別名,獲取和設定變數值都針對相同的變數物件。雖然 PowerShell 允許您將任何運算式轉換為
[ref],但除了變數之外,它的功能類似于常規賦值,因此無效。[1]
這個答案有更深入的資訊[ref]。
簡化示例:
正確使用[ref]: 與變數:
- 不使用函式的插圖:
PS> $foo = 42; $bar = [ref] $foo; $bar.Value; $foo
43 # OK, incrementing the .Value of $bar updated the value of $foo
語法說明:別名變數必須直接轉換為[ref]. 因此,嘗試將其用作變數型別約束是行不通的:[ref] $bar = $foo
- 具有 .NET API 的真實示例
[int]::TryParse(),其第二個引數是一個out引數:
# Declare a variable to use with [ref]
# Note: No need to type the variable.
$intVal = $null
# Pass the variable via [ref] to receive the out parameter value
# of [int]::TryParse()
$null = [int]::TryParse('42', [ref] $intVal)
# $intVal now contains 42
毫無意義地使用[ref]:與任何其他運算式,例如物件屬性:[1]
PS> $foo = [pscustomobject] @{ prop = 42 }; $bar = [ref] $foo.prop; $bar.Value; $foo.prop
42 # !! $foo's value was NOT updated.
因此,您的選擇是:
如果
Add-Five無法修改,請使用輔助變數作為按參考引數。class MyClass{ $a = 0 } Function Add-Five { param([ref]$value) $value.Value =5 } $myObj = [MyClass]::new() # Create and use an aux. variable. $aux = $myObj.a Add-Five ([ref] $aux) # Update the property via the updated aux. variable. $myObj.a = $aux # myObj.a now contains 5否則:
讓
Add-Five輸出(回傳)新值并將其分配給屬性:class MyClass{ $a = 0 } Function Add-Five { param($value) # regular parameter (variable) $value 5 # output the new value } $myObj = [MyClass]::new() # Pass the property value and assign back to it. $myObj.a = Add-Five $myObj.a # myObj.a now contains 5正如Mathias R. Jessen建議的那樣,將一個
[MyClass]實體作為一個整體傳遞給Add-Five并讓它在那里更新屬性:class MyClass{ $a = 0 } Function Add-Five { param([MyClass] $MyObj) # expect a [MyClass] instance as a whole $MyObj.a = 5 # update the property directly } $myObj = [MyClass]::new() Add-Five $myObj # myObj.a now contains 5
[ref][1]概念性的about_Ref幫助主題中顯示了另一種(盡管是奇異的)用法,但它與創建對存盤在別處的值的參考無關;相反,它使用[ref]實體作為常規變數的替代方案,以允許以語法上更方便的方式從后代范圍更新它(使用常規變數,您必須使用Get-Variable/ Set-Variablecmdlet)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/415810.html
標籤:
下一篇:使用復選框在gui中復制檔案
