是否PSCustomObject知道其属性的添加顺序?
# Order of properties
$o21 = New-Object PSCustomObject |
Add-Member NoteProperty a2 2 -passThru |
Add-Member NoteProperty a1 1 -passThru
$o21 | fl
a2 : 2
a1 : 1
$o12 = New-Object PSCustomObject |
Add-Member NoteProperty a1 1 -passThru |
Add-Member NoteProperty a2 2 -passThru
$o12 | fl
a1 : 1
a2 : 2
Run Code Online (Sandbox Code Playgroud)
我想读这个订单.怎么样?
要在PowerShell中获取对象属性的有序列表,可以Properties通过隐藏的psobjectmemberset属性访问集合:
PS C:\> $o12.psobject.Properties
MemberType : NoteProperty
IsSettable : True
IsGettable : True
Value : 1
TypeNameOfValue : System.Int32
Name : a1
IsInstance : True
MemberType : NoteProperty
IsSettable : True
IsGettable : True
Value : 2
TypeNameOfValue : System.Int32
Name : a2
IsInstance : True
Run Code Online (Sandbox Code Playgroud)
Name如果您只想使用以下命令列出属性名称,请展开该属性Select-Object:
$PropertyNames = $o12.psobject.Properties |Select-Object -ExpandProperty Name
Run Code Online (Sandbox Code Playgroud)
或使用属性枚举(PowerShell 3.0+):
$PropertyNames = $o12.psobject.Properties.Name
Run Code Online (Sandbox Code Playgroud)