res*_*sgh 5 c# parameters powershell pipeline properties
根据MSDN:
"必须在公共非静态字段或属性上声明参数.应在属性上声明参数.该属性必须具有公共集访问器,如果指定了ValueFromPipeline或ValueFromPipelineByPropertyName关键字,则该属性必须具有公共get访问器."
为什么我必须在我的cmdlet ValueFromPipeline参数中声明get访问器?据我所知,PowerShell只需要将它们的值放入,而不是将它们读出来.谢谢(顺便说一句,我只是好奇这个行为:)).
PowerShell会读取标有或 的参数的默认值,以便在分配从管道获得的新值之前进行备份。ValueFromPipelineValueFromPipelineByPropertyName
考虑以下 cmdlet:
New-Post -Title <string>
Set-Post -InputObject <Post> -Title <string>
Run Code Online (Sandbox Code Playgroud)
适用以下情况:
New-Post对象具有一个属性PostTitleInputObjectcmdlet上的属性标记Set-Post为ValueFromPipeline = trueTitlecmdlet上的属性标Set-Post有ValueFromPipelineByPropertyName = true。将它们与以下命令组合起来:
New-Post -Title "Foo" | Set-Post
Run Code Online (Sandbox Code Playgroud)
Set-Post并在cmdlet属性的 get 访问器上设置断点会Title产生以下堆栈跟踪:

如您所见,在将 cmdlet上的属性与来自管道的对象上的相应属性的值CmdletParameterBinderController.GetDefaultParameterValue绑定的过程中调用该方法。TitleSet-Post
| 归档时间: |
|
| 查看次数: |
529 次 |
| 最近记录: |