如何使用 PowerShell Get-Member cmdlet [英] How to use PowerShell Get-Member cmdlet
问题描述
一个新手问题:
命令:
[Math] | Get-Member
返回 System.RuntimeType
的所有成员.这是为什么?
Returns all members of System.RuntimeType
. Why is that?
还有命令:
Get-Member -InputObject [Math]
返回System.String
的所有成员.如果 [Math]
在这里被解释为字符串,我怎样才能使它成为一个数学对象?
Returns all members of System.String
. If [Math]
is interpreted as string here, how can I make it a math object?
另外,Get-member
是否接受任何位置参数?我怎么知道?
Also, does Get-member
takes any positional parameters? How can I tell?
推荐答案
您正在从 [Math] 获得 System.RuntimeType,因为它就是这样.它是一个类类型,而不是该特定类型的对象.您实际上还没有构建 [Math] 对象.如果您键入:
You are getting a System.RuntimeType from [Math] because that is what it is. It's a Class type as opposed to an object of that particular type. You haven't actually constructed a [Math] object. You will get the same output if you typed:
[String] | gm
但是,如果您从 String 类型构造一个字符串对象,您将获得字符串成员:
However, if you constructed a string object from the String type, you would get the string members:
PS C:\> [String]("hi") | gm
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone()
CompareTo Method System.Int32 CompareTo(Object value), System.Int32 CompareTo(String strB)
Contains Method System.Boolean Contains(String value)
CopyTo Method System.Void CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIn...
etc...
因为 System.Math 只有静态成员,所以不能构造它的对象.要查看它的成员,您可以使用 System.RuntimeType 的 GetMembers() 函数:
Since System.Math has only static members, you can't construct an object of it. To see it's members you can use the GetMembers() function of System.RuntimeType:
[Math].GetMethods()
您可以使用 format-* cmdlet 之一来格式化输出:
You can use one of the format-* cmdlets to format the output:
[Math].GetMethods() | format-table
哦,我应该补充一点,要调用静态成员之一,您可以这样做:
Oh, and I should add, to invoke one of the static members, you would do it like this:
[Math]::Cos(1.5)
这篇关于如何使用 PowerShell Get-Member cmdlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!