¿Es posible adjuntar un evento a un PSObject?
Di que tengo un objeto psíquico como este:
$o=New-Object PSObject -Property @{"value"=0}
Add-Member -MemberType ScriptMethod -Name "Sqrt" -Value {
echo "the square root of $($this.value) is $([Math]::Round([Math]::Sqrt($this.value),2))"
} -inputObject $o
¿Es posible adjuntar un evento para que el método Sqrt () se ejecute cuando cambie el atributo de valor? es decir:
PS>$o.value=9
Producirá
the square root of 9 is 3
actualizarSegún la respuesta de @Richard, esta es la receta de trabajo:
$o=New-Object PSObject -property @{"_val"=1}
Add-Member -MemberType ScriptMethod -Name "Sqrt" -Value {
write-host "the square root of $($this._val) is $([Math]::Round([Math]::Sqrt($this._val),2))"
} -inputObject $o
Add-Member -MemberType ScriptProperty -Name 'val' -Value{ $this._val } -SecondValue { $this._val=$args[0];$this.Sqrt() } -inputObject $o