2014-04-16 19 views
5

Neden parantezler arasında bir ifade gömülürken iki farklı üye listesi aldığımı merak ediyorum, ör. gl -stack. Parantez olmadan, ifade tamamen değerlendirilir ve sonuç bir kerede bir sonraki boru hattı bileşenine iletilir. Ancak, parantez içinde, koleksiyondaki tek nesneler, koleksiyonun kendisi için değil, koleksiyondaki nesneler için Get-Member çağrılır. Lütfen Get-Location -Stack ile bir örnek için aşağıdaki PowerShell etkileşimine bakın."(gl -stack)" ve "gl -stack" arasındaki fark

Şimdiden teşekkürler!

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;} 

cevap

3

Get-Location -Stack gördüğünüz gibi PathInfoStack nesnesi döndürür. Bu nesne, ICollection uygulayan Stack<T>'dan türetilmiştir. () içine bir ifade koyduğunuzda PowerShell bu ifadeyi değerlendirir. Sonuç bir koleksiyon ise, o zaman yinelenir ve çıktılanır. Bu basit işlevin aynısını görebilirsiniz:

PS> function GetArray() { ,@(1,2,3) } 
PS> GetArray | Foreach {$_.GetType().FullName} 
System.Object[] 
PS> (GetArray) | Foreach {$_.GetType().FullName} 
System.Int32 
System.Int32 
System.Int32 
+0

Teşekkür ederiz. Yukarıdaki prosedürü açıklayan MSDN'de 'resmi' bir yardım sayfasına veya benzer bir bağlantıya sahip misiniz? Çok aradım ama bunun için herhangi bir bilgi bulamadı. – DAXaholic

+1

İşte yapbozun bir parçası. Get-Location kullanımı gibi çıktıları çıktılarını boru hattına yazmak için kullanılan bir WriteObject yöntemi vardır. İşte, http://msdn.microsoft.com/en-us/library/ms568370(v=vs.85).aspx adresindeki aşırı yüklenmelerden birinin bağlantısı. Cmdlet'in nesnenin "olduğu gibi" boru hattına yerleştirilip yerleştirilmeyeceğini veya PowerShell nesneyi (koleksiyon olduğu varsayılarak) numaralandırıp numaralandırmayacağını seçer ve ardından sayılan öğeleri boru hattına yerleştirir. BTW bir değişkene atarsanız aynı gruplandırma ifadesi davranışını görürsünüz. '$ arr = GetArray; $ arr | % {$ _. GetType(). Name} ' –

İlgili konular