方法定義了一個對象可以做什么事情。當你把一個對象輸出在控制臺時,它的屬性可能會被轉換成可視的文本。但是它的方法卻不可見。列出一個對象的所有方法可是使用Get-Member命令,給“MemeberType”參數 傳入“Method”:
代碼如下:
PS C:Powershell> $Host | Get-Member -MemberType Method
TypeName: System.Management.Automation.Internal.Host.InternalHost
Name MemberType Definition
---- ---------- ----------
EnterNestedPrompt Method System.Void EnterNestedPrompt()
Equals Method bool Equals(System.Object obj)
ExitNestedPrompt Method System.Void ExitNestedPrompt()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
NotifyBeginApplication Method System.Void NotifyBeginApplication()
NotifyEndApplication Method System.Void NotifyEndApplication()
PopRunspace Method System.Void PopRunspace()
PushRunspace Method System.Void PushRunspace(runspace runspace)
SetShouldExit Method System.Void SetShouldExit(int exitCode)
ToString Method string ToString()
過濾內部方法
Get-Memeber列出了一個對象定義的所有方法,但并不是所有的方法都有用,有些方法的的用處非常有限。
Get_ 和 Set_ 方法
所有名稱以”get_”打頭的方法都是為了給對應的屬性返回一個值。例如”get_someInfo()”方法的作用就是返回屬性someInfo的值,因此可以直接通過屬性調用。
代碼如下:
PS C:Powershell> $Host.Version
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
PS C:Powershell> $Host.get_Version()
Major Minor Build Revision
新聞熱點
疑難解答