powershell 数组的方法(Clear、ForEach、Where 等)在哪里定义?
Where methods (Clear, ForEach, Where, etc.) of powershell array are defined?
根据offical documentPowerShell中关于数组的介绍,为数组定义了几种方法,例如Clear()、ForEach()、Where()等。以下代码测试了这些方法:
$arr = 1..2
$arr.Clear()
$arr.Length
write "--------------------------------------------------"
$arr = 1..2
$arr.ForEach({$_ + 1})
write "--------------------------------------------------"
$arr = 65..90
$arr.Where({($_ % 2) -eq 0})
输出:
2
--------------------------------------------------
2
3
--------------------------------------------------
66
68
70
72
74
76
78
80
82
84
86
88
90
很好!并且,诸如 ForEach() 之类的方法有许多未在此处测试的重载。
但是这些方法是在哪里定义的呢?我的意思是,包含这些方法定义的 class 是什么?据我所知,.net core 中没有定义这些方法。 (我使用 PowerShell 7)
Clear()
是 System.Array
实现的 IList
接口的一部分,它是 PowerShell 中集合 (System.Object[]
) 的基本类型。
要查看所有可用的本机方法,请使用:
$arr.PSObject.Methods
# or
Get-Member -InputObject $arr -MemberType Methods
但是: Where()
和 ForEach()
"magic" methods were introduced in v4 and are actually PowerShell-specific extension methods (as more performant alternatives to ForEach-Object
and Where-Object
), defined in System.Management.Automation.EnumerableOps
. Have a look at the source:
internal static object Where(IEnumerator enumerator, ScriptBlock expressionSB, WhereOperatorSelectionMode selectionMode, int numberToReturn)
internal static object ForEach(IEnumerator enumerator, object expression, object[] arguments)
为什么这么说据我所知,.net core 中没有定义这些方法。 (我使用 PowerShell 7)?真的:
$PSVersionTable.PSVersion | Out-Default
$arr = 1..2
$arr.GetType() | Get-Member -MemberType Methods -Static
Major Minor Patch PreReleaseLabel BuildLabel
----- ----- ----- --------------- ----------
7 0 3
TypeName: System.Object[]
Name MemberType Definition
---- ---------- ----------
AsReadOnly Method static System.Collections.ObjectModel.ReadOnlyCollection[T] …
BinarySearch Method static int BinarySearch[T](T[] array, int index, int length,…
Clear Method static void Clear(array array, int index, int length)
ConstrainedCopy Method static void ConstrainedCopy(array sourceArray, int sourceInd…
ConvertAll Method static TOutput[] ConvertAll[TInput, TOutput](TInput[] array,…
Copy Method static void Copy(array sourceArray, array destinationArray, …
CreateInstance Method static array CreateInstance(type elementType, int length), s…
Empty Method static T[] Empty[T]()
Equals Method static bool Equals(System.Object objA, System.Object objB)
Exists Method static bool Exists[T](T[] array, System.Predicate[T] match)
Fill Method static void Fill[T](T[] array, T value), static void Fill[T]…
Find Method static T Find[T](T[] array, System.Predicate[T] match)
FindAll Method static T[] FindAll[T](T[] array, System.Predicate[T] match)
FindIndex Method static int FindIndex[T](T[] array, System.Predicate[T] match…
FindLast Method static T FindLast[T](T[] array, System.Predicate[T] match)
FindLastIndex Method static int FindLastIndex[T](T[] array, System.Predicate[T] m…
ForEach Method static void ForEach[T](T[] array, System.Action[T] action)
IndexOf Method static int IndexOf(array array, System.Object value), static…
LastIndexOf Method static int LastIndexOf(array array, System.Object value), st…
new Method System.Object[] new(int )
ReferenceEquals Method static bool ReferenceEquals(System.Object objA, System.Objec…
Resize Method static void Resize[T]([ref] T[] array, int newSize)
Reverse Method static void Reverse(array array), static void Reverse(array …
Sort Method static void Sort(array array), static void Sort(array keys, …
TrueForAll Method static bool TrueForAll[T](T[] array, System.Predicate[T] mat…
根据offical documentPowerShell中关于数组的介绍,为数组定义了几种方法,例如Clear()、ForEach()、Where()等。以下代码测试了这些方法:
$arr = 1..2
$arr.Clear()
$arr.Length
write "--------------------------------------------------"
$arr = 1..2
$arr.ForEach({$_ + 1})
write "--------------------------------------------------"
$arr = 65..90
$arr.Where({($_ % 2) -eq 0})
输出:
2
--------------------------------------------------
2
3
--------------------------------------------------
66
68
70
72
74
76
78
80
82
84
86
88
90
很好!并且,诸如 ForEach() 之类的方法有许多未在此处测试的重载。
但是这些方法是在哪里定义的呢?我的意思是,包含这些方法定义的 class 是什么?据我所知,.net core 中没有定义这些方法。 (我使用 PowerShell 7)
Clear()
是 System.Array
实现的 IList
接口的一部分,它是 PowerShell 中集合 (System.Object[]
) 的基本类型。
要查看所有可用的本机方法,请使用:
$arr.PSObject.Methods
# or
Get-Member -InputObject $arr -MemberType Methods
但是: Where()
和 ForEach()
"magic" methods were introduced in v4 and are actually PowerShell-specific extension methods (as more performant alternatives to ForEach-Object
and Where-Object
), defined in System.Management.Automation.EnumerableOps
. Have a look at the source:
internal static object Where(IEnumerator enumerator, ScriptBlock expressionSB, WhereOperatorSelectionMode selectionMode, int numberToReturn)
internal static object ForEach(IEnumerator enumerator, object expression, object[] arguments)
为什么这么说据我所知,.net core 中没有定义这些方法。 (我使用 PowerShell 7)?真的:
$PSVersionTable.PSVersion | Out-Default
$arr = 1..2
$arr.GetType() | Get-Member -MemberType Methods -Static
Major Minor Patch PreReleaseLabel BuildLabel ----- ----- ----- --------------- ---------- 7 0 3 TypeName: System.Object[] Name MemberType Definition ---- ---------- ---------- AsReadOnly Method static System.Collections.ObjectModel.ReadOnlyCollection[T] … BinarySearch Method static int BinarySearch[T](T[] array, int index, int length,… Clear Method static void Clear(array array, int index, int length) ConstrainedCopy Method static void ConstrainedCopy(array sourceArray, int sourceInd… ConvertAll Method static TOutput[] ConvertAll[TInput, TOutput](TInput[] array,… Copy Method static void Copy(array sourceArray, array destinationArray, … CreateInstance Method static array CreateInstance(type elementType, int length), s… Empty Method static T[] Empty[T]() Equals Method static bool Equals(System.Object objA, System.Object objB) Exists Method static bool Exists[T](T[] array, System.Predicate[T] match) Fill Method static void Fill[T](T[] array, T value), static void Fill[T]… Find Method static T Find[T](T[] array, System.Predicate[T] match) FindAll Method static T[] FindAll[T](T[] array, System.Predicate[T] match) FindIndex Method static int FindIndex[T](T[] array, System.Predicate[T] match… FindLast Method static T FindLast[T](T[] array, System.Predicate[T] match) FindLastIndex Method static int FindLastIndex[T](T[] array, System.Predicate[T] m… ForEach Method static void ForEach[T](T[] array, System.Action[T] action) IndexOf Method static int IndexOf(array array, System.Object value), static… LastIndexOf Method static int LastIndexOf(array array, System.Object value), st… new Method System.Object[] new(int ) ReferenceEquals Method static bool ReferenceEquals(System.Object objA, System.Objec… Resize Method static void Resize[T]([ref] T[] array, int newSize) Reverse Method static void Reverse(array array), static void Reverse(array … Sort Method static void Sort(array array), static void Sort(array keys, … TrueForAll Method static bool TrueForAll[T](T[] array, System.Predicate[T] mat…