Skip to content

Commit 0498174

Browse files
committed
main
1 parent f1fa055 commit 0498174

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

docs/document/Skill/PowerShell/docs/Language/PSCustomObject.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ The must-know is **`[PSCustomObject]` maps to the same type as `[psobject]`, `Sy
1717
The only valid usage is marking one HashTable as `System.Management.Automation.PSCustomObject`. Other possible usages of `[PSCustomObject]` is bascially pointless.
1818

1919
> [!WARNING]
20-
> Do not use `PSCustomObject` case-sensitive keys matters, name of extended properties are not case-sensitive, use `HashTable` instead.
20+
> Do not use `PSCustomObject` if case-sensitive keys matter, name of extended properties are not case-sensitive, use `HashTable` instead.
2121
2222
## Creation
2323

2424
PSCustomObject borrows the syntax from HashTable with a casting.
2525

2626
```ps1
2727
$john = [PSCustomObject] @{
28-
Name = 'John Smith'
28+
Name = 'John Smith'
2929
Age = 18
3030
}
3131
```
@@ -39,7 +39,7 @@ $table = @{
3939
Foo = 'foo'
4040
}
4141
42-
$obj = [PSCustomObject]$table # [!code highlight]
42+
$obj = [PSCustomObject]$table # [!code highlight]
4343
```
4444

4545
Or use `New-Object`, but this might be slower.
@@ -52,9 +52,28 @@ $obj = New-Object -TypeName PSObject -Property $table
5252

5353
```ps1
5454
$john = [PSCustomObject] @{
55-
Name = 'John Smith'
55+
Name = 'John Smith'
5656
Age = 18
5757
}
5858
5959
$smith = $john.psobject.Copy()
6060
```
61+
62+
## Add New Members
63+
64+
You can't directly add new properties after declaration using dot accessor.
65+
One should use `Add-Member` to append new members.
66+
67+
```ps1
68+
$obj | Add-Member -MemberType NoteProperty -Name PropName -Value Value
69+
```
70+
71+
> [!NOTE]
72+
> All members of a `[pscustomobject]` should be *extended properties*, you can inspect them using `$obj.psextended`
73+
74+
## Enumerating Properties
75+
76+
```ps1
77+
$obj.psobject.properties | foreach { $_.Value = 0 }
78+
```
79+

docs/document/Skill/PowerShell/docs/Object Manipulation/5.Select.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The following is a overview:
1010
- Take a count from start or end with `-First` or `-Last`.
1111
- Skip a count from start or end `-Skip` or `-SkipLast`.
1212
- Cherry-Pick one or more items by zero-based index.
13-
- Distinct items selected from pipline.
13+
- Distinct items selected from pipeline.
1414

1515
> [!TIP]
1616
> Use `select` alias for `Select-Object`.
@@ -104,7 +104,7 @@ gps | select -ExpandProperty Name
104104
`-ExpandProperty` can be used together with `-Property`, selected properties by `-Property` will be added as **NoteProperty** to the selected `-ExpandProperty`.
105105
106106
> [!WARNING]
107-
> This approach mutates the selected property instead of generating a new object.
107+
> This approach mutates the selected property for `-ExpandProperty` instead of generating a new object.
108108
109109
```ps1
110110
$player = @{
@@ -119,14 +119,14 @@ $player = @{
119119
}
120120
121121
# Adds a NoteProperty `Status` with the same value from $player to $player.Partner
122-
$partner = $john | select Status -ExpandProperty Partner
122+
$partner = $player | select Status -ExpandProperty Partner
123123
124-
[object]::ReferenceEquals($partner, $john.Child) # True
124+
[object]::ReferenceEquals($partner, $john.Partner) # True # [!code highlight]
125125
126-
$child | gm -MemberType NoteProperty # Status
126+
$partner | gm -MemberType NoteProperty # Status
127127
128128
$partner.Status # Empowered # . accessor prefers ETS property
129-
$partner['Status'] # Poisoned
129+
$partner['Status'] # Poisoned, from the hashtable
130130
```
131131
132132
> [!NOTE]

docs/document/Skill/PowerShell/docs/Type System/Extended Type System.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,46 @@ There's two kinds of intrinsic members in PowerShell
1414
- Object views: the mapping of a certain object.
1515
- Property and methods: intrinsic properties and methods.
1616

17-
All objects in PowerShell have five **object views** members:
17+
### Object Views
1818

19-
- `psobject`: a `MemberSet` containing reflection source of members.
19+
Powershell distributes members to three parts for each object: *extended*, *adapted* and *base*.
2020

21+
- `psobject`: a `MemberSet` containing reflection source of members.
22+
- `psobject.baseobject`: a reference of the object itself.
23+
- `psobject.properties`: all properties from all sources(extended, adapted and base)
24+
- `psobject.members`: all members(including properties) from all sources(extended, adapted and base)
25+
- `psobject.methods`: all methods from type definition(such as `.NET` type), including raw methods of properties and indexers.
2126
```ps1
2227
$foo = 'I am foo'
2328
[object]::ReferenceEquals($foo, $foo.psobject.BaseObject) # True
2429
```
25-
- `psbase`: a `MemberSet` containing members of the object being wrapped.
26-
30+
- `psbase`: a `MemberSet` containing members of the **original object**.
31+
- the base members are defined ahead of time such as `.NET` types.
32+
- `psadapted`: a `MemberSet` containing adapted members added by ETS.
33+
- members appended dedicatedly for a type **by all means**.
2734
```ps1
28-
$foo = 'I am foo'
29-
[object]::ReferenceEquals($foo.ToString, $foo.psbase.ToString) # True
35+
$x = [System.Xml.XmlDocument]::new()
36+
$x.LoadXml("<foo><bar>123</bar></foo>")
37+
$x.psadapted.foo.bar # 123 # [!code highlight]
3038
```
31-
32-
- `psadapted`: a `MemberSet` containing adapted members added by ETS.
3339
- `psextended`: a `MemberSet` containing extended members **added at runtime**.
40+
- properties appended dynamically(conditionally) are extended property, such as `PSPath` for `[FileSystemInfo]` created from item cmdlets.
41+
- properties of a `[pscustomobject]` are extended properties.
3442
- `pstypenames`: a `CodeProperty` equivalent to `psobject.TypeNames`. Returns a collection containing the type names of inheritance chain.
3543
3644
```ps1
3745
$foo = 'I am foo'
3846
[object]::ReferenceEquals($foo.psobject.TypeNames, $foo.pstypenames) # True
3947
```
4048
49+
#### Member Priority
50+
51+
1. `psextended`
52+
2. `psadapted`
53+
3. `psbase`
54+
55+
### Intrinsic Methods
56+
4157
Intrinsic methods and properties are to mimic singular object and collection in a same form.
4258
- `Where`: a method for filtering or slicing by condition. See [Where](../Object Manipulation/3.Where.md)
4359
- `ForEach`: a method to perform iteration with certain logic or perform casting all items to target type. See [ForEach](../Object Manipulation/4.ForEach.md#Intrinsic%20ForEach)

0 commit comments

Comments
 (0)