powershell - 可以使用Get-Member检查集合的方法和属性,而不是检查集合项(默认值)吗?

标签 powershell

假设$coll是对象的集合。 $coll | Get-Member显示集合项的成员。我不知道它是如何工作的,无论它是检查第一个项目然后停止还是显示所有项目成员的并集。但是,它会检查项目,而不是收集对象本身。通常,这就是我们想要的。

但是,如果我想检查馆藏本身怎么办?我仍然可以这样做还是必须使用.NET反射功能?

最佳答案

愚蠢的我,当然可以。我只需要使用-InputObject参数,而不是管道-Get-Member -InputObject $coll而不是$coll | Get-Member

C:\WINDOWS\System32> $x = @([pscustomobject]@{a = 1})
C:\WINDOWS\System32> $x|Get-Member


   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
a           NoteProperty int a=1


C:\WINDOWS\System32> Get-Member -InputObject $x


   TypeName: System.Object[]

Name           MemberType            Definition
----           ----------            ----------
Count          AliasProperty         Count = Length
Add            Method                int IList.Add(System.Object value)
Address        Method                System.Object&, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a...
Clear          Method                void IList.Clear()
Clone          Method                System.Object Clone(), System.Object ICloneable.Clone()
CompareTo      Method                int IStructuralComparable.CompareTo(System.Object other, System.Collections.ICo...
Contains       Method                bool IList.Contains(System.Object value)
CopyTo         Method                void CopyTo(array array, int index), void CopyTo(array array, long index), void...
Equals         Method                bool Equals(System.Object obj), bool IStructuralEquatable.Equals(System.Object ...
Get            Method                System.Object Get(int )
GetEnumerator  Method                System.Collections.IEnumerator GetEnumerator(), System.Collections.IEnumerator ...
GetHashCode    Method                int GetHashCode(), int IStructuralEquatable.GetHashCode(System.Collections.IEqu...
GetLength      Method                int GetLength(int dimension)
GetLongLength  Method                long GetLongLength(int dimension)
GetLowerBound  Method                int GetLowerBound(int dimension)
GetType        Method                type GetType()
GetUpperBound  Method                int GetUpperBound(int dimension)
GetValue       Method                System.Object GetValue(Params int[] indices), System.Object GetValue(int index)...
IndexOf        Method                int IList.IndexOf(System.Object value)
Initialize     Method                void Initialize()
Insert         Method                void IList.Insert(int index, System.Object value)
Remove         Method                void IList.Remove(System.Object value)
RemoveAt       Method                void IList.RemoveAt(int index)
Set            Method                void Set(int , System.Object )
SetValue       Method                void SetValue(System.Object value, int index), void SetValue(System.Object valu...
ToString       Method                string ToString()
Item           ParameterizedProperty System.Object IList.Item(int index) {get;set;}
IsFixedSize    Property              bool IsFixedSize {get;}
IsReadOnly     Property              bool IsReadOnly {get;}
IsSynchronized Property              bool IsSynchronized {get;}
Length         Property              int Length {get;}
LongLength     Property              long LongLength {get;}
Rank           Property              int Rank {get;}
SyncRoot       Property              System.Object SyncRoot {get;}


C:\WINDOWS\System32>

关于powershell - 可以使用Get-Member检查集合的方法和属性,而不是检查集合项(默认值)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58677043/

相关文章:

windows - aws s3 sync --recursive 在 Windows 中不起作用

windows - 在 PowerShell 中批量重命名

android - 保留通过 adb shell 发送的 JSON 引号

powershell - 根据行值过滤CSV文件

powershell - Powershell保持RemoteDrive session 打开

PowerShell - 从特定文件夹复制特定文件

powershell - 使用 Powershell 进行 Azure 资源管理器网络安全组配置

.net - 在使用 select-xml 应用 XPath 表达式后,是否有可靠的方法来获取所选元素的名称

powershell - 将数组内容写入foreach

powershell - 通过 PowerShell 设置 REG_NONE 值