PetSerAl .
, PowerShell System.Object[] :
PowerShell :
, () [object[]] ([System.Object[]]), System.Object - .NET, .
, [object[]], [string], [int], [datetime] $null .
$arr = 'hi', 42, (Get-Date), $null
:
System.Object[] - , .
PowerShell , :
: , ( , ), ; :.
$intArray = 1, 2
$intArray[0] = 'one'
[System.Object[]] , [int], - .
PowerShell .NET, , , , :
[int[]] $intArray = 1, 2
$intArray[0] = 'one'
, - $intArray = [int[]] (1, 2) - , , (, $intArray = 'one', 'two' ).
: [int[]] 1, 2 , , , ([int[]] 1), 2, [object[]], [int[]] 1.
@(...) [1] , , , , .
PowerShell , , :
PowerShell , :
[string[]] $a = 'one', 'two'
$a[0] = 1
[string[]] $a = 'one', 'two'
$a[0] = [int] 1
. , - [int] 1 - , . - - , - , , PowerShell, - . [2]
, , [string[]] - .
, () , ,
[int[]] $arr = 1, 2; $arr[0] = 'one' # error
" " [object[]]:
PowerShell "" +.
(), [object[]], :
$intArray = [int[]] (1, 2)
($intArray + 4).GetType().Name
$intArray += 3
([int[]] ($intArray + 4)).GetType().Name
[int[]] $intArray = (1, 2)
$intArray += 3
[object[]]:
, ( ) [object[]], :
function foo { return [int[]] (1, 2) }
(foo).GetType().Name
function foo { return [System.Collections.ArrayList] (1, 2) }
(foo).GetType().Name
([System.Collections.ArrayList] (1, 2) | Write-Output).GetType().Name
, PowerShell : ; , ( 1).
, PowerShell , - , (...), [object[]].
PowerShell , IEnumerable , , IDictionary .
, PowerShell hashtables ([hashtable]) - ( PSv3 + [ordered] @{...}), [System.Collections.Specialized.OrderedDictionary]) ( -) , .GetEnumerator().
PowerShell :
: , PowerShell , .
function foo { ,1 }
(foo).GetType().Name
( ,1 | Write-Output ).GetType().Name
@( (,1) | Write-Output ).GetType().Name
, @(...): , ( ) : < > , [object[]] 1 .
, , , [object[]], :
$a1 = 1, 2; $a2 = @( $a1 ); [object]::ReferenceEquals($a1, $a2)
$false, , $a1 $a2 .
:
(...), - , :
- (- ), ; , ([System.Collections.ArrayList] (1, 2)) -is [System.Collections.ArrayList] ([int[]] (1,2)) -is [int[]] $true - .
- (- ), ; .:
(&{ , 1 }) -is [int] $true ( ), (& { [int[]] (1, 2) }) -is [object[]] ( [int[]] [object[]]) $true, & .
() $(...), , :
$(,1) -is [int] $([System.Collections.ArrayList] (1, 2)) -is [object[]] $true.
script:
, , .
, , PowerShell [object[]].
, , , PowerShell :
function foo { , [System.Collections.ArrayList] (1, 2) }
$arrayList = foo
$arrayList.GetType().Name
PSv4 + Write-Output -NoEnumerate:
function foo { write-output -NoEnumerate ([System.Collections.ArrayList] (1, 2)) }
$arrayList = foo
$arrayList.GetType().Name
[1] , @(...) , , .
PSv5.1 ( , , ) , , - @() @() - . .
@(...) :
* : (@( 1 ) (@( 1, 2 ))). , ,: 1, 2 vs. , 1.
* , - @(...) ( , ).
* , $(...) @(...) .
[2] PetSerAl , , PowerShell :
# Define a simple type that implements an interface
# and a method that has 2 overloads.
Add-Type '
public interface I { string M(); }
public class C : I {
string I.M() { return "I.M()"; }
public string M() { return "C.M()"; }
public string M(int i) { return "C.M(int)"; }
public string M(object o) { return "C.M(object)"; }
}
'
# Instantiate the type and use casts to distinguish between
# the type and its interface, and to target a specific overload.
$C = New-Object C
$C.M()
([I]$C).M() # cast is respected
$C.M(1)
$C.M([object]1) # cast is respected