是否可以将事件附加到PSObject? [英] Is it possible to attach an event to a PSObject?
问题描述
$ o =新对象PSObject -Property @ {value = 0}
添加成员-MemberType ScriptMethod -NameSqrt-Value {
echo$($ this.value)的平方根是$([Math] :: Round([Math ] :: Sqrt($ this.value),2))
} -inputObject $ o
是否可以附加事件,以便在value属性更改时执行方法Sqrt()?即:
PS> $ o.value = 9
将生成
9的平方根为3
更新
根据@Richard的回答,这是工作食谱:
$ o =新对象PSObject -property @ {_ val= 1}
添加成员-MemberType ScriptMethod -NameSqrt-Value {
write-host$($ this._val)的平方根是$([Math] :: Round([Math] :: Sqrt($ this。 _val),2))
} -inputObject $ o
添加成员-MemberType ScriptProperty -Name'val'-Value {$ this._val} -SecondValue { $ this._val = $ args [0]; $ this.Sqrt()} -inputObject $ o
而不是使值
a NoteProperty
使其成为 ScriptProperty
,这包括定义单独的get和set方法,而不是直接修改一个字段。
$ theObject | Add-Member -MemberType ScriptProperty -Name'Value'
-Value {$ this._value}
-SecondValue {$ this._value = $ args [0]; $ this.Sqrt}
(价值
定义get方法 SecondValue
集合。)
注意,由于PowerShell不提供封装数据的任何能力,呼叫者仍然可以访问基础字段。在C#(或其他.NET语言)中编码自定义类型,并使用 Add-Type
可以避免这种情况,但不大可能值得,除非您真的有呼叫者将不遵守规则。
第二期
在ScriptProperty中没有输出管道(任何输出都被丢弃),所以 echo
(作为 Write-Output
的别名)不会做任何有用的事情使用 Write-Host
替换它。一般来说,在获取或设置属性(包括输出)中的副作用是不好的做法(使用它们时期望低开销)。
Say I've a psobject like this :
$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
Is it possible to attach an event so that the method Sqrt() is executed when the value attribute change ? ie :
PS>$o.value=9
will produce
the square root of 9 is 3
update
As per @Richard answer this is the working recipe :
$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
Rather than making value
a NoteProperty
make it a ScriptProperty
, this includes defining separate get and set methods that are called rather than directly modifying a field.
$theObject | Add-Member -MemberType ScriptProperty -Name 'Value'
-Value{ $this._value }
-SecondValue { $this._value = $args[0]; $this.Sqrt }
(Value
defines the get method, SecondValue
the set.)
Note as PowerShell doesn't provide any ability to encapsulate data, the underlying field is still accessible to callers. Coding a custom type in C# (or other .NET language) and use of Add-Type
can avoid this, but is unlikely to be worth it unless you really have callers who will not follow the rules.
Second Issue
In a ScriptProperty there is no output pipe (any output is thrown away), so echo
(being an alias for Write-Output
) won't do anything useful. Replacing it with Write-Host
works. In general side effects in a property get or set (including output) are poor practice (there is an expectation of low overhead when using them).
这篇关于是否可以将事件附加到PSObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!