PowerShell 使用 Get-Member 列出继承的方法 [英] PowerShell list inherited methods with Get-Member
问题描述
我正在探索哪些管道功能也可用作方法.例如...<代码>@('1', '2', '3', '2') |其中 {$_ -in @('2')} 将返回两个 '2'.不过我也可以...@('1', '2', '3', '2').Where({$_ -in @('2')})
并得到相同的结果.
I am exploring what pipeline functionality is also available as methods. For example...
@('1', '2', '3', '2') | where {$_ -in @('2')}
will return the two '2'. But I can also do...
@('1', '2', '3', '2').Where({$_ -in @('2')})
and get the same result.
我也想Group
,而<代码>@('1', '2', '3', '2') |group {$_.Count -gt 1} 没有做我想要的实际分组,它确实......某事.但@('1', '2', '3', '2').Group({$_.Count -gt 1})
失败,方法调用失败,因为 [System.String] 不包含名为Group"的方法.
I also want to Group
, and while
@('1', '2', '3', '2') | group {$_.Count -gt 1}
doesn't do the actual grouping I want, it does... something. But
@('1', '2', '3', '2').Group({$_.Count -gt 1})
fails with Method invocation failed because [System.String] does not contain a method named 'Group'.
所以这让我寻找可用的内在方法.<代码>@('1') |Get-Member -MemberType 方法 -Force |Format-Table 甚至不包括 Where
而方法却在那里.所以我假设它继承了那个方法.但我不知道如何包含继承的方法.我虽然 -Force
会这样做,但它没有,-View
也没有提供更多细节.
So that got me looking for what IS available as intrinsic methods.
@('1') | Get-Member -MemberType Method -Force | Format-Table
doesn't even include Where
and yet the Method is there. So I assume it is inheriting that method. But I don't know how to include inherited Methods. I though -Force
would do it but it doesn't, nor does -View
provide any more detail.
有没有办法探索这些内在方法,例如 .Where()
?或许切切地,有没有办法将 Group 作为一种方法而不是管道?
Is there a way to explore these intrinsic methods such as .Where()
? And perhaps tangentially, is there a way to Group as a method rather than with the pipeline?
推荐答案
Dabombber 的回答很有帮助,但让我试试给出系统概述:
.Where()
和 .ForEach()
数组方法 是半官方称为内在成员的实例;自 PowerShell 7.1 起,不存在其他此类内部数组处理(集合处理)方法
The .Where()
and .ForEach()
array methods are instances of what is semi-officially known as intrinsic members; no other such intrinsic array-processing (collection-processing) methods exist as of PowerShell 7.1
内在成员是PowerShell引擎在任何类型的对象上公开的成员(属性和方法)- 除非,如果对象具有同名的本机 .NET 类型成员,则优先.
Intrinsic members are members (properties and methods) that the PowerShell engine exposes on objects of any type - except if the object has a native .NET type member of the same name, which takes precedence.
发现限制,截至 PowerShell 7.1/2020 年末:
Discovery limitations, as of PowerShell 7.1 / late 2020:
无法通过
获取会员
- GitHub 提案 #11798 旨在引入此功能.
- GitHub proposal #11798 aims to introduce this ability.
Tab 补全仅适用于 .Where()
和 .ForEach()
(在集合值上),但不适用于其他内部成员,例如.psobject
属性(见下文).
Tab completion works only for .Where()
and .ForEach()
(on collection values), but not for other intrinsic members, such as the .psobject
property (see below).
没有讨论内在成员的概念性帮助主题.
There is no conceptual help topic that discusses intrinsic members.
- GitHub 文档问题 #5627 建议引入一个.
- GitHub docs issue #5627 suggests introducing one.
简而言之:在撰写本文时,您无法以编程方式发现内在成员.
In short: as of this writing, you cannot discover intrinsic members programmatically.
内部成员列表:
支持统一处理集合(数组)和标量的内在成员:
PowerShell 添加
.Count
和.Length
属性(彼此的别名)甚至标量(假设它们没有同名的类型本机属性),非空标量明智地报告1
(例如,(42).Count
), 和$null
报告0
($null.Count
)
PowerShell adds
.Count
and.Length
properties (aliases of each other) even to scalars (assuming they don't have type-native properties of the same name), with an non-null scalar sensibly reporting1
(e.g.,(42).Count
), and$null
reporting0
($null.Count
)
- 陷阱:
在
Set-StrictMode -Version 2
或更高版本有效时访问这些属性意外导致语句终止错误,因为引擎将它们视为非存在;GitHub 问题 #2798 中讨论了这个长期存在的错误.
- Pitfalls:
Accessing these properties while
Set-StrictMode -Version 2
or higher is in effect unexpectedly causes statement-terminating errors, because the engine treats them as non-existent; this long-standing bug is discussed in GitHub issue #2798.
IEnumerable
LINQ 方法返回的实例本身并不是集合,对它们调用.Count
会触发枚举 并返回.Count来自每个枚举元素的 code> 属性值(默认为
1
);例如,[Linq.Enumerable]::Range(1,3).Count
返回数组1, 1, 1
而不是3
.IEnumerable
instances such as returned by LINQ methods aren't collections per se, and calling.Count
on them triggers enumeration and returns a.Count
property value from each enumerated element instead (which defaults to1
); e.g.,[Linq.Enumerable]::Range(1,3).Count
returns array1, 1, 1
instead of3
.同样,PowerShell 允许您索引甚至标量(同样,除非被类型本机索引器抢占,例如
XmlElement
一>);例如,(42)[0]
和(42)[-1]
都返回42
,即标量本身.Similarly, PowerShell allows you to index even into a scalar (again, unless preempted by a type-native indexer, such as that of
XmlElement
); e.g.,(42)[0]
and(42)[-1]
both return42
, i.e. the scalar itself.收集处理内部成员:
Collection-processing intrinsic members:
这些是(仅)上面讨论的
.Where()
和.ForEach()
方法.
These are (only) the
.Where()
and.ForEach()
methods discussed above.
注意,为了统一处理集合和标量,这些方法也适用于标量;例如,
(42).ForEach({ $_ + 1 })
产生43
.Note that, in the the interest of unified treatment of collections and scalars, these methods also work on scalars; e.g.,
(42).ForEach({ $_ + 1 })
yields43
.陷阱:
在 Windows PowerShell(自 PowerShell [Core] 7+ 中修复)中,某些 类型的标量 - 特别是
[pscustomobject]
和[xml]
- 不要不提供这些方法,这应该被认为是一个bug.
In Windows PowerShell (since fixed in PowerShell [Core] 7+), some types of scalars - notably
[pscustomobject]
and[xml]
- do not provide these methods, which should be considered a bug.
System.Collections.Generic.List
集合类型有自己的.ForEach()
方法,因此隐藏了 PowerShell 的方法;这种类型本机方法既不支持使用$_
也不支持产生输出.The
System.Collections.Generic.List<T>
collection type has its own.ForEach()
method that therefore shadows PowerShell's; this type-native method neither supports the use of$_
nor producing output.reflection 的内部成员(在
$null
上不可用):Intrinsic members for reflection (not available on
$null
):.psobject
属性 是对任何对象(例如其属性列表)进行反射的丰富来源;例如,(Get-Date).psobject.Properties
列出了有关System.DateTime
.
The
.psobject
property is a rich source of reflection on any object, such as its list of properties; e.g.,(Get-Date).psobject.Properties
lists metadata about all public properties of instances ofSystem.DateTime
.
.pstypenames
(也可用作.psobject.TypeNames
)列出所有 ETS(扩展类型系统) 与实例关联的类型名称;默认情况下,该属性包含对象的 .NET 类型及其基类型的全名..pstypenames
(also available as.psobject.TypeNames
) lists all ETS (Extended Type System) type names associated with an instance; by default, the property contains the full names of the object's .NET type and its base type(s)..psbase
、.psextended
和psadapted
属性返回类型成员的分类子集,即 .NET 原生、ETS 添加和改编成员.The
.psbase
,.psextended
, andpsadapted
properties return categorized subsets of type members, namely .NET-native, ETS-added, and adapted members.Adapted 成员是从不同的数据表示中显示信息的成员,就好像它们是本机类型成员一样,特别是在 CIM (WMI) 和 PowerShell 对 XML DOM 的改编.
Adapted members are members that surface information from a different data representation as if they were native type members, notably in the context of CIM (WMI) and PowerShell's adaptation of the XML DOM.
例如,
[xml]
(System.Xml.XmlDocument
) 实例具有所有三个类别的成员;try$xml = ([xml] '<someElement>some text</someElement>');'-- .psbase:';$xml.psbase;'-- .psextended';$xml.psextended;'-- .psadapted';$xml.psadapted
这篇关于PowerShell 使用 Get-Member 列出继承的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!