这个
[ref]
班
一
关键字
)是到
.NET API
ref
out
参数
.
很少在纯PowerShell代码中使用,最好避免在那里使用
简而言之:
-
[参考]
仅在PowerShell中有效
,它真正为给定变量创建别名
对象
-
而PowerShell允许你施放
任何
[参考]
,用任何东西
另外
而不是它作用的变量
,因此无效。
[1]
This answer
有更多关于
[参考]
正确使用
[参考]
:带有
变量
:
PS> $foo = 42; $bar = [ref] $foo; ++$bar.Value; $foo
43 # OK, incrementing the .Value of $bar updated the value of $foo
语法注释
直接地
到
[参考]
. 因此,尝试将其用作变量类型约束会
工作:
[ref] $bar = $foo
# 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
毫无意义地使用
[参考]
任何其他
表示
:
[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
-
-
允许
输出
(返回)新值并将其分配给属性:
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]
例子
作为一个整体
让它更新那里的属性:
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
[参考]
如概念图所示
about_Ref
; 相反,它使用
可供替代的
以允许对其进行更新
从子范围
Get-Variable
/
Set-Variable
cmdlet)。