问题描述
根据offical document关于PowerShell中数组的信息,为数组定义了几种方法,例如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()之类的方法具有许多未在此处进行测试的重载。
但是这些方法在哪里定义?我的意思是,包含这些方法的定义的类是什么?据我所知,这些方法未在.net核心中定义。 (我使用PowerShell 7)
解决方法
Clear()
是IList
实现的System.Array
接口的一部分,该接口是PowerShell中集合(System.Object[]
)的基本类型。
要查看所有可用的本机方法,请使用:
$arr.PSObject.Methods
# or
Get-Member -InputObject $arr -MemberType Methods
但是:Where()
和ForEach()
“ magic”方法是在v4中引入的,实际上是特定于PowerShell的 extension 方法(作为更高性能的替代方法) ForEach-Object
中定义的Where-Object
和System.Management.Automation.EnumerableOps
)。看看source:
internal static object Where(IEnumerator enumerator,ScriptBlock expressionSB,WhereOperatorSelectionMode selectionMode,int numberToReturn)
internal static object ForEach(IEnumerator enumerator,object expression,object[] arguments)
,
但是这些方法在哪里定义?我的意思是,包含这些方法的定义的类是什么?
$arr = @(1..10)
使用$arr.GetType()
给出
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
这是Array类。您可以在下面的链接及其所具有的方法中找到有关数组类的更多信息
https://docs.microsoft.com/en-us/dotnet/api/system.array?view=netcore-3.1
,为什么要声明据我所知,.net核心中未定义这些方法。 (我使用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 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,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…