13
私はPowershellを初めて使いました。関数内から[ref]変数の値を出力する方法を工夫しています。ここでpowershell:[ref]変数から値を書き込む方法
は私のテストコードです:
function testref([ref]$obj1) {
$obj1.value = $obj1.value + 5
write-host "the new value is $obj1"
$obj1 | get-member
}
$foo = 0
"foo starts with $foo"
testref([ref]$foo)
"foo ends with $foo"
次のように私はこのテストから取得が出力されます。あなたは私が望んでいた$ obj1の価値を得られないことに気付くでしょう。私もwrite-hostへの呼び出しで$ obj1.valueを渡そうとしましたが、同じ応答が生成されました。
PS > .\testref.ps1
foo starts with 0
the new value is System.Management.Automation.PSReference
TypeName: System.Management.Automation.PSReference
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Value Property System.Object Value {get;set;}
foo ends with 5
はい、それでした。ありがとう! – Denis
@Denis - あなたの問題を解決すれば答えとして受け入れてください! – manojlds
どうすればいいですか? – Denis