PS版本:5.1
期望的
我希望的工作$obj = [PSCustomObject]@{
PSTypeName = 'MyObject'
con = 'local'
}
add-member -MemberType ScriptMethod -InputObject $obj -Name MyMethod -Value {
param([parameter(ValueFromPipeline=$true)]$x)
begin {write-host 'begin'}
process{write-host "$($this.con): $x"}
end {write-host 'end'}
}
1..5 | $obj.MyMethod
给予
begin
local: 1
local: 2
local: 3
local: 4
local: 5
end
但给出错误
At line:1 char:8
+ 1..5 | $obj.MyMethod
+ ~~~~~~~~~~~~~
Expressions are only allowed as the first element of a pipeline.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
试过了
什么不起作用,因为:idk,tl;dr 原因?1..5 | %{$obj.MyMethod($_)}
什么不起作用,因为:开始/结束
$obj = [PSCustomObject]@{
PSTypeName = 'MyObject'
con = 'local'
}
add-member -MemberType ScriptMethod -InputObject $obj -Name MyMethod -Value {
param($x)
write-host "$($this.con): $x"
}
1..5 | %{$obj.MyMethod($_)}
什么不起作用,因为: $this 不再附加
$obj = [PSCustomObject]@{
PSTypeName = 'MyObject'
con = 'local'
MyMethodSB = {
param()
begin {write-host 'begin'}
process{write-host "$($obj.con): $_"}
end {write-host 'end'}
}
}
1..5 | &$obj.MyMethodSB
什么不起作用,因为:idk,也许 Stepper 的范围过早死亡
$obj = [PSCustomObject]@{
PSTypeName = 'MyObject'
con = 'local'
Stepper = $null
}
add-member -MemberType ScriptMethod -InputObject $obj -Name MyMethod -Value {
param($x)
if($null -eq $this.Stepper){
$t = $this
$sb = {
param($o)
begin {write-host 'begin'}
process{write-host "$($o.con): $_"}
end {write-host 'end'}
}
$this.Stepper = {&$sb $t}.GetSteppablePipeline()
$this.Stepper.Begin($true)
}
$this.Stepper.Process($x)
}
1..5 | %{$obj.MyMethod($_)}
$obj.Stepper.End()
$obj.Stepper = $null
差不多好了
什么有点工作$Data = @{
ItemNo1 = @(
@{loc = 'WH'; qty = 20}
@{loc = 'DK'; qty = 0}
@{loc = 'ST1'; qty = 3}
@{loc = 'ST2'; qty = 2}
)
ItemNo2 = @(
@{loc = 'WH'; qty = 6}
@{loc = 'DK'; qty = 0}
)
ItemNo3 = @(
@{loc = 'WH'; qty = 100}
@{loc = 'ST1'; qty = 5}
@{loc = 'DK'; qty = 0}
)
ItemNo4 = @(
@{loc = 'WH'; qty = 0}
@{loc = 'DK'; qty = 0}
@{loc = 'ST2'; qty = 15}
)
ItemNo5 = @(
@{loc = 'WH'; qty = 0}
@{loc = 'DK'; qty = 15}
)
}
$QueryParameter = [PSCustomObject]@{Value = $null}
function MockDataQuery{
$Item = $QueryParameter.Value
foreach($iDat in $Data.$Item){
" $Item;$($iDat.loc);$($iDat.qty)"
}
}
$InvObj = [PSCustomObject]@{
PSTypeName = 'Data.Connection'
con = 'item.inventory'
}
add-member -MemberType ScriptMethod -InputObject $InvObj -Name FetchItemData -Value {
$p = $this
$qPipe = [PSCustomObject]@{
PSTypeName = 'Data.Connection.QueryPipe'
parent = $p
qHeader = 'Item;Location;Quantity'
query = 'select * from inventory where item = ?'
param = $null
}
add-member -MemberType ScriptMethod -InputObject $qPipe -Name begin -Value {
param([bool]$Header=$true)
write-host "connect to: $($this.parent.con)"
write-host "compile command: $($this.query)"
$this.param = $QueryParameter
write-host 'begin transaction'
if($Header){
write-host " $($this.qHeader)"
}
}
add-member -MemberType ScriptMethod -InputObject $qPipe -Name process -Value {
param($itm)
$this.param.Value = $itm
MockDataQuery | write-host
}
add-member -MemberType ScriptMethod -InputObject $qPipe -Name end -Value {
write-host 'end transaction'
write-host 'dispose command'
write-host 'close connection'
}
return $qPipe
}
1..5 | %{"ItemNo$_"} | &{
begin {$stp = $InvObj.FetchItemData();$stp.begin()}
process{$stp.process($_)}
end {$stp.end()}
}
虽然这似乎是一个功能模型,但如果有一个解决方案不需要连接对象的用户每次使用对象的方法时都必须在他们的末端编写开始/处理/结束脚本,那就更好了.