在ObjectX []数组中使用ObjectX的方法?

时间:2017-08-01 18:01:58

标签: powershell

我错误地输入了一个命令并且很困惑为什么它仍然有效。例如:

PS [73] D:\ > $x = "test","me"

PS [74] D:\ > get-typename $x
String[]

PS [75] D:\ > $x.replace("e","x")
txst
mx

这是否意味着我总是可以在String []上调用String方法(或任何objtype),并且在不使用foreach的情况下将它神奇地应用于每个元素?是否有一些(未记录的?)隐式演员正在进行?

编辑:更加困惑。当Object和Object []具有相同的方法名称时会发生什么?示例:第一个示例很简单; Int32有一个ToUint16,但是Int32 []没有,所以隐含的foreach是合乎逻辑的:

PS [119] C:\ > [int32[]]$x = 1,2,3

PS [120] C:\ > $x.ToUInt16([System.Globalization.DateTimeFormatInfo]::CurrentInfo)
1
2
3

PS [121] C:\ > $x | % {$_.ToUInt16([System.Globalization.DateTimeFormatInfo]::CurrentInfo)}
1
2
3

但是在这里,两种类型都有ToString()方法。而神奇暗示着foreach dissapears:

PS [122] C:\ > $x | % { $_.ToString() }
1
2
3

PS [123] C:\ > $x.ToString()
System.Int32[]

1 个答案:

答案 0 :(得分:-3)

简答:是的,String数组对象上存在Replace方法。

答案很长:

PS C:\Users\TheIncorrigible1> $x | GM | ft -AutoSize -Wrap

TypeName      Name                        MemberType Definition                  
--------      ----                        ---------- ----------                  
System.String Clone                           Method System.Object Clone(),      
                                                     System.Object               
                                                     ICloneable.Clone()          
System.String CompareTo                       Method int CompareTo(System.Object 
                                                     value), int                 
                                                     CompareTo(string strB), int 
                                                     IComparable.CompareTo(System
                                                     .Object obj), int IComparabl
                                                     e[string].CompareTo(string  
                                                     other)                      
System.String Contains                        Method bool Contains(string value) 
System.String CopyTo                          Method void CopyTo(int             
                                                     sourceIndex, char[]         
                                                     destination, int            
                                                     destinationIndex, int count)
System.String EndsWith                        Method bool EndsWith(string        
                                                     value), bool                
                                                     EndsWith(string value,      
                                                     System.StringComparison     
                                                     comparisonType), bool       
                                                     EndsWith(string value, bool 
                                                     ignoreCase, cultureinfo     
                                                     culture)                    
System.String Equals                          Method bool Equals(System.Object   
                                                     obj), bool Equals(string    
                                                     value), bool Equals(string  
                                                     value,                      
                                                     System.StringComparison     
                                                     comparisonType), bool IEquat
                                                     able[string].Equals(string  
                                                     other)                      
System.String GetEnumerator                   Method System.CharEnumerator       
                                                     GetEnumerator(), System.Coll
                                                     ections.IEnumerator IEnumera
                                                     ble.GetEnumerator(), System.
                                                     Collections.Generic.IEnumera
                                                     tor[char] IEnumerable[char].
                                                     GetEnumerator()             
System.String GetHashCode                     Method int GetHashCode()           
System.String GetType                         Method type GetType()              
System.String GetTypeCode                     Method System.TypeCode             
                                                     GetTypeCode(),              
                                                     System.TypeCode             
                                                     IConvertible.GetTypeCode()  
System.String IndexOf                         Method int IndexOf(char value),    
                                                     int IndexOf(char value, int 
                                                     startIndex), int            
                                                     IndexOf(char value, int     
                                                     startIndex, int count), int 
                                                     IndexOf(string value), int  
                                                     IndexOf(string value, int   
                                                     startIndex), int            
                                                     IndexOf(string value, int   
                                                     startIndex, int count), int 
                                                     IndexOf(string value,       
                                                     System.StringComparison     
                                                     comparisonType), int        
                                                     IndexOf(string value, int   
                                                     startIndex,                 
                                                     System.StringComparison     
                                                     comparisonType), int        
                                                     IndexOf(string value, int   
                                                     startIndex, int count,      
                                                     System.StringComparison     
                                                     comparisonType)             
System.String IndexOfAny                      Method int IndexOfAny(char[]       
                                                     anyOf), int                 
                                                     IndexOfAny(char[] anyOf,    
                                                     int startIndex), int        
                                                     IndexOfAny(char[] anyOf,    
                                                     int startIndex, int count)  
System.String Insert                          Method string Insert(int           
                                                     startIndex, string value)   
System.String IsNormalized                    Method bool IsNormalized(), bool Is
                                                     Normalized(System.Text.Norma
                                                     lizationForm                
                                                     normalizationForm)          
System.String LastIndexOf                     Method int LastIndexOf(char        
                                                     value), int                 
                                                     LastIndexOf(char value, int 
                                                     startIndex), int            
                                                     LastIndexOf(char value, int 
                                                     startIndex, int count), int 
                                                     LastIndexOf(string value),  
                                                     int LastIndexOf(string      
                                                     value, int startIndex), int 
                                                     LastIndexOf(string value,   
                                                     int startIndex, int count), 
                                                     int LastIndexOf(string      
                                                     value,                      
                                                     System.StringComparison     
                                                     comparisonType), int        
                                                     LastIndexOf(string value,   
                                                     int startIndex,             
                                                     System.StringComparison     
                                                     comparisonType), int        
                                                     LastIndexOf(string value,   
                                                     int startIndex, int count,  
                                                     System.StringComparison     
                                                     comparisonType)             
System.String LastIndexOfAny                  Method int LastIndexOfAny(char[]   
                                                     anyOf), int                 
                                                     LastIndexOfAny(char[]       
                                                     anyOf, int startIndex), int 
                                                     LastIndexOfAny(char[]       
                                                     anyOf, int startIndex, int  
                                                     count)                      
System.String Normalize                       Method string Normalize(), string N
                                                     ormalize(System.Text.Normali
                                                     zationForm                  
                                                     normalizationForm)          
System.String PadLeft                         Method string PadLeft(int          
                                                     totalWidth), string         
                                                     PadLeft(int totalWidth,     
                                                     char paddingChar)           
System.String PadRight                        Method string PadRight(int         
                                                     totalWidth), string         
                                                     PadRight(int totalWidth,    
                                                     char paddingChar)           
System.String Remove                          Method string Remove(int           
                                                     startIndex, int count),     
                                                     string Remove(int           
                                                     startIndex)                 
System.String Replace                         Method string Replace(char         
                                                     oldChar, char newChar),     
                                                     string Replace(string       
                                                     oldValue, string newValue)  
System.String Split                           Method string[] Split(Params       
                                                     char[] separator), string[] 
                                                     Split(char[] separator, int 
                                                     count), string[]            
                                                     Split(char[] separator,     
                                                     System.StringSplitOptions   
                                                     options), string[]          
                                                     Split(char[] separator, int 
                                                     count,                      
                                                     System.StringSplitOptions   
                                                     options), string[]          
                                                     Split(string[] separator,   
                                                     System.StringSplitOptions   
                                                     options), string[]          
                                                     Split(string[] separator,   
                                                     int count,                  
                                                     System.StringSplitOptions   
                                                     options)                    
System.String StartsWith                      Method bool StartsWith(string      
                                                     value), bool                
                                                     StartsWith(string value,    
                                                     System.StringComparison     
                                                     comparisonType), bool       
                                                     StartsWith(string value,    
                                                     bool ignoreCase,            
                                                     cultureinfo culture)        
System.String Substring                       Method string Substring(int        
                                                     startIndex), string         
                                                     Substring(int startIndex,   
                                                     int length)                 
System.String ToBoolean                       Method bool IConvertible.ToBoolean(
                                                     System.IFormatProvider      
                                                     provider)                   
System.String ToByte                          Method byte IConvertible.ToByte(Sys
                                                     tem.IFormatProvider         
                                                     provider)                   
System.String ToChar                          Method char IConvertible.ToChar(Sys
                                                     tem.IFormatProvider         
                                                     provider)                   
System.String ToCharArray                     Method char[] ToCharArray(),       
                                                     char[] ToCharArray(int      
                                                     startIndex, int length)     
System.String ToDateTime                      Method datetime IConvertible.ToDate
                                                     Time(System.IFormatProvider 
                                                     provider)                   
System.String ToDecimal                       Method decimal IConvertible.ToDecim
                                                     al(System.IFormatProvider   
                                                     provider)                   
System.String ToDouble                        Method double IConvertible.ToDouble
                                                     (System.IFormatProvider     
                                                     provider)                   
System.String ToInt16                         Method int16 IConvertible.ToInt16(S
                                                     ystem.IFormatProvider       
                                                     provider)                   
System.String ToInt32                         Method int IConvertible.ToInt32(Sys
                                                     tem.IFormatProvider         
                                                     provider)                   
System.String ToInt64                         Method long IConvertible.ToInt64(Sy
                                                     stem.IFormatProvider        
                                                     provider)                   
System.String ToLower                         Method string ToLower(), string    
                                                     ToLower(cultureinfo culture)
System.String ToLowerInvariant                Method string ToLowerInvariant()   
System.String ToSByte                         Method sbyte IConvertible.ToSByte(S
                                                     ystem.IFormatProvider       
                                                     provider)                   
System.String ToSingle                        Method float IConvertible.ToSingle(
                                                     System.IFormatProvider      
                                                     provider)                   
System.String ToString                        Method string ToString(), string To
                                                     String(System.IFormatProvide
                                                     r provider), string IConvert
                                                     ible.ToString(System.IFormat
                                                     Provider provider)          
System.String ToType                          Method System.Object               
                                                     IConvertible.ToType(type    
                                                     conversionType,             
                                                     System.IFormatProvider      
                                                     provider)                   
System.String ToUInt16                        Method uint16 IConvertible.ToUInt16
                                                     (System.IFormatProvider     
                                                     provider)                   
System.String ToUInt32                        Method uint32 IConvertible.ToUInt32
                                                     (System.IFormatProvider     
                                                     provider)                   
System.String ToUInt64                        Method uint64 IConvertible.ToUInt64
                                                     (System.IFormatProvider     
                                                     provider)                   
System.String ToUpper                         Method string ToUpper(), string    
                                                     ToUpper(cultureinfo culture)
System.String ToUpperInvariant                Method string ToUpperInvariant()   
System.String Trim                            Method string Trim(Params char[]   
                                                     trimChars), string Trim()   
System.String TrimEnd                         Method string TrimEnd(Params       
                                                     char[] trimChars)           
System.String TrimStart                       Method string TrimStart(Params     
                                                     char[] trimChars)           
System.String Chars            ParameterizedProperty char Chars(int index) {get;}
System.String Length                        Property int Length {get;}