PowerShell 使用 Get-Member 列出继承的方法 [英] PowerShell list inherited methods with Get-Member

查看:48
本文介绍了PowerShell 使用 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 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 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 reporting 1 (e.g., (42).Count), and $null reporting 0 ($null.Count)

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆