我只是想知道为什么在圆括号(例如gl -stack
)之间嵌入表达式时,我会得到两个不同的成员列表。似乎没有括号,表达式将被完全计算,结果立即传递给下一个管道组件。但是,使用括号,集合中的单个对象将一个接一个地传递,以便为集合中的对象而不是集合本身调用Get-Member
。有关与PowerShell的示例,请参见下面的Get-Location -Stack
交互。
提前感谢!
PS C:\temp\loc1> pushd
PS C:\temp\loc1> pushd ..\loc2
PS C:\temp\loc2> gl -stack
Path
----
C:\temp\loc1
C:\temp\loc1
PS C:\temp\loc2> gl -stack | gm
TypeName: System.Management.Automation.PathInfoStack
Name MemberType Definition
---- ---------- ----------
Clear Method System.Void Clear()
Contains Method bool Contains(System.Management.Automation.PathInfo...
CopyTo Method System.Void CopyTo(System.Management.Automation.Pat...
Equals Method bool Equals(System.Object obj)
GetEnumerator Method System.Collections.Generic.Stack`1+Enumerator[[Syst...
GetHashCode Method int GetHashCode()
GetType Method type GetType()
Peek Method System.Management.Automation.PathInfo Peek()
Pop Method System.Management.Automation.PathInfo Pop()
Push Method System.Void Push(System.Management.Automation.PathI...
ToArray Method System.Management.Automation.PathInfo[] ToArray()
ToString Method string ToString()
TrimExcess Method System.Void TrimExcess()
Count Property System.Int32 Count {get;}
Name Property System.String Name {get;}
PS C:\temp\loc2> (gl -stack) | gm
TypeName: System.Management.Automation.PathInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Drive Property System.Management.Automation.PSDriveInfo Drive {get;}
Path Property System.String Path {get;}
Provider Property System.Management.Automation.ProviderInfo Provider {...
ProviderPath Property System.String ProviderPath {get;}
发布于 2014-04-16 22:33:00
正如您所看到的,Get-Location -Stack
返回一个PathInfoStack
对象。该对象是从实现Stack<T>
的ICollection派生的。当您将表达式放入()
中时,PowerShell计算该表达式。如果结果是一个集合,那么它将被迭代并输出。您可以在这个简单的函数中看到同样的内容:
PS> function GetArray() { ,@(1,2,3) }
PS> GetArray | Foreach {$_.GetType().FullName}
System.Object[]
PS> (GetArray) | Foreach {$_.GetType().FullName}
System.Int32
System.Int32
System.Int32
https://stackoverflow.com/questions/23119007
复制相似问题