Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchen committed Dec 5, 2024
1 parent 5a13b4d commit db6c634
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
26 changes: 20 additions & 6 deletions docs/document/Powershell/docs/Language/PSCustomObject.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# PSCustomObject

Differ from HashTable, `[PSCustomObject]` is a representation for object literal.
It
`PSCustomObject` is a minimal object representation over `System.Object`.
Custom properties are allowed as `NoteProperty`.

In the following example, the `PSCustomObject` created has the same member as default `[object]` except the NoteProperty `Foo`.

```ps1
[object]::new() | gm
[PSCustomObject]@{ Foo = 'I am a NoteProperty!' } | gm
```

## Why Do We Need it

Differ from HashTable as a dictionary, `[PSCustomObject]` is a representation for object literal.
The must-know is **`[PSCustomObject]` maps to the same type as `[psobject]`, `System.Management.Automation.PSObject`.**
The only valid usage is marking one HashTable as `System.Management.Automation.PSCustomObject`. Other possible usages of `[PSCustomObject]` is bascially pointless.

> [!WARNING]
> Do not use `PSCustomObject` case-sensitive keys matters, name of extended properties are not case-sensitive, use `HashTable` instead.
## Creation

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

## Copy a PSCustomObject

- Shallow copy
## Shallow Copy

```ps1
$john = [PSCustomObject] @{
Name = 'John Smith'
Age = 18
}
$smith = $john.PSObject.Copy()
$smith = $john.psobject.Copy()
```
4 changes: 3 additions & 1 deletion docs/document/Powershell/docs/Type System/1.Overview.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Overview

- Intrinsic Members
- Intrinsic Members: builtin members for all objects in PowerShell.
- Extended Type System: how PowerShell append extra members over a PowerShell object.
- Native Types: types always available in PowerShell.
13 changes: 13 additions & 0 deletions docs/document/Powershell/docs/Type System/Intrinsic Members.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Intrinsic Members

All objects in PowerShell have five intrinsic members:

- `psobject`: Reflection source of members.
- `psbase`: a `MemberSet` containing members of the object being wrapped.
- `psadapted`: adapted members added by ETS.
- `psextended`: a `MemberSet` containing extended members **added at runtime**.
- `pstypenames`: equivalent to `psobject.TypeNames`. A collection containing the type names of inheritance chain.

```ps1
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Type Casting & Checking

## Casting

Prepend a type accelerator or type name before a variable or expression.

```ps1
[System.IO.FileInfo[]](gci -file)
```

### Safe Casting & Convertion

`-as` acts the similar as `as` in `C#`, it tries to convert object to target type, returns `$null` if failed.
The left operand can be:
- type name as string
- type accelerator
- type accelerator name as string

```ps1
$foo -as [object]
(Get-Date) -as 'string' # convert to date string
(Get-Date) -as 'System.String'
```

> [!NOTE]
> All objects can be casted to `string`.
## Checking

`-is` and `-isnot` acts the same as `is` and `is not` in `C#`.

0 comments on commit db6c634

Please sign in to comment.