[英]How to use line breaks in PowerShell Method-Chaining
I am trying to use a Retry Service that is written in the fluent-api pattern.我正在尝试使用以 fluent-api 模式编写的重试服务。 The methods return the service and allow a chaining of methods.
这些方法返回服务并允许方法链接。 However even though i am using --> ` <-- i see a lot of errors as shown below.
然而,即使我正在使用 --> ` <-- 我也看到了很多错误,如下所示。
Is there any workaround or other possibility to not write everything into one line?是否有任何解决方法或其他可能性不将所有内容写入一行? (I already checked the methods names and return types)
(我已经检查了方法名称和返回类型)
Unfortunately about_Methods doesn't seem to have a clarification on method chaining and its parsing rules.不幸的是, about_Methods似乎没有对方法链及其解析规则进行澄清。 If you want to chain multiple methods on new lines, the dot
.
如果你想在新行上链接多个方法,点
.
has to be at the end of each statement then a newline is allowed.必须在每个语句的末尾,然后允许换行。 The backticks are not needed .
不需要反引号。
In example:例如:
[powershell]::Create().
AddScript({ "hello $args" }).
AddArgument('world').
Invoke()
Another way to chain method calls is to use the ForEach-Object
command (alias %
).链接方法调用的另一种方法是使用
ForEach-Object
命令(别名%
)。 This relies on the parameter set with the -MemberName
parameter (implicitly by passing a string as the 1st argument).这依赖于使用
-MemberName
参数设置的参数(通过将字符串作为第一个参数隐式传递)。
PowerShell 7+ even lets you write the pipe symbol |
PowerShell 7+甚至可以让您编写 pipe 符号
|
on a new line:在新的一行:
[powershell]::Create()
|% AddScript { "hello $args" }
|% AddArgument 'world'
|% Invoke
If there are multiple method arguments, you have to separate them by ,
(parentheses are unnecessary).如果有多个方法arguments,则必须用
,
分隔(不需要括号)。
For PS 5 and below you have to use a slightly different syntax because the pipe symbol has to be on the same line as the previous command:对于PS 5 及以下版本,您必须使用略有不同的语法,因为 pipe 符号必须与上一个命令位于同一行:
[powershell]::Create() |
% AddScript { "hello $args" } |
% AddArgument 'world' |
% Invoke
Is this a better way than using member access operator .
这是比使用成员访问运算符更好的方法吗
.
? ? I don't think so, it's just a different way.
我不这么认为,这只是一种不同的方式。 IMO it does look more consistent compared to regular PowerShell commands.
IMO 与常规 PowerShell 命令相比,它看起来确实更一致。 Performance might be even worse than
.
性能可能比
.
but for high level code it propably doesn't matter (I haven't measured).但对于高级代码,它可能无关紧要(我没有测量过)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.