Skip to content

Commit db6c634

Browse files
committed
main
1 parent 5a13b4d commit db6c634

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

docs/document/Powershell/docs/Language/PSCustomObject.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
# PSCustomObject
22

3-
Differ from HashTable, `[PSCustomObject]` is a representation for object literal.
4-
It
3+
`PSCustomObject` is a minimal object representation over `System.Object`.
4+
Custom properties are allowed as `NoteProperty`.
5+
6+
In the following example, the `PSCustomObject` created has the same member as default `[object]` except the NoteProperty `Foo`.
7+
8+
```ps1
9+
[object]::new() | gm
10+
[PSCustomObject]@{ Foo = 'I am a NoteProperty!' } | gm
11+
```
12+
13+
## Why Do We Need it
14+
15+
Differ from HashTable as a dictionary, `[PSCustomObject]` is a representation for object literal.
16+
The must-know is **`[PSCustomObject]` maps to the same type as `[psobject]`, `System.Management.Automation.PSObject`.**
17+
The only valid usage is marking one HashTable as `System.Management.Automation.PSCustomObject`. Other possible usages of `[PSCustomObject]` is bascially pointless.
18+
19+
> [!WARNING]
20+
> Do not use `PSCustomObject` case-sensitive keys matters, name of extended properties are not case-sensitive, use `HashTable` instead.
521
622
## Creation
723

@@ -32,15 +48,13 @@ Or use `New-Object`, but this might be slower.
3248
$obj = New-Object -TypeName PSObject -Property $table
3349
```
3450

35-
## Copy a PSCustomObject
36-
37-
- Shallow copy
51+
## Shallow Copy
3852

3953
```ps1
4054
$john = [PSCustomObject] @{
4155
Name = 'John Smith'
4256
Age = 18
4357
}
4458
45-
$smith = $john.PSObject.Copy()
59+
$smith = $john.psobject.Copy()
4660
```
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Overview
22

3-
- Intrinsic Members
3+
- Intrinsic Members: builtin members for all objects in PowerShell.
4+
- Extended Type System: how PowerShell append extra members over a PowerShell object.
5+
- Native Types: types always available in PowerShell.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Intrinsic Members
2+
3+
All objects in PowerShell have five intrinsic members:
4+
5+
- `psobject`: Reflection source of members.
6+
- `psbase`: a `MemberSet` containing members of the object being wrapped.
7+
- `psadapted`: adapted members added by ETS.
8+
- `psextended`: a `MemberSet` containing extended members **added at runtime**.
9+
- `pstypenames`: equivalent to `psobject.TypeNames`. A collection containing the type names of inheritance chain.
10+
11+
```ps1
12+
13+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Type Casting & Checking
2+
3+
## Casting
4+
5+
Prepend a type accelerator or type name before a variable or expression.
6+
7+
```ps1
8+
[System.IO.FileInfo[]](gci -file)
9+
```
10+
11+
### Safe Casting & Convertion
12+
13+
`-as` acts the similar as `as` in `C#`, it tries to convert object to target type, returns `$null` if failed.
14+
The left operand can be:
15+
- type name as string
16+
- type accelerator
17+
- type accelerator name as string
18+
19+
```ps1
20+
$foo -as [object]
21+
(Get-Date) -as 'string' # convert to date string
22+
(Get-Date) -as 'System.String'
23+
```
24+
25+
> [!NOTE]
26+
> All objects can be casted to `string`.
27+
28+
## Checking
29+
30+
`-is` and `-isnot` acts the same as `is` and `is not` in `C#`.

0 commit comments

Comments
 (0)