Uso di classi e metodi statici
Non tutte le classi .NET Framework possono essere create con New-Object
. Ad esempio, se si tenta di creare un oggetto System.Environment o System.Math con New-Object
, verranno visualizzati i messaggi di errore seguenti:
New-Object System.Environment
New-Object : Constructor not found. Cannot find an appropriate constructor for
type System.Environment.
At line:1 char:11
+ New-Object <<<< System.Environment
New-Object System.Math
New-Object : Constructor not found. Cannot find an appropriate constructor for
type System.Math.
At line:1 char:11
+ New-Object <<<< System.Math
Questi errori si verificano perché non è possibile creare un nuovo oggetto da queste classi. Queste classi sono librerie di riferimento di metodi e proprietà che non modificano lo stato. Non devono essere create, ma solo usate. Le classi e i metodi come questi sono denominati classi statiche perché non vengono create, eliminate o modificate. Per chiarire il concetto verranno forniti esempi che usano le classi statiche.
Recupero dei dati dell'ambiente con System.Environment
In genere, il primo passaggio quando si lavora con un oggetto in Windows PowerShell consiste nell'usare Get-Member per scoprire quali membri contiene. Con le classi statiche, il processo è leggermente diverso perché la classe effettiva non è un oggetto.
Riferimento alla classe System.Environment statica
Si può fare riferimento a una classe statica racchiudendo il nome della classe tra parentesi quadre. Ad esempio, si può fare riferimento a System.Environment digitando il nome tra parentesi quadre. In questo modo vengono visualizzate alcune informazioni di tipo generico:
[System.Environment]
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False Environment System.Object
Nota
Come accennato in precedenza, Windows PowerShell antepone automaticamente 'System.' per digitare i nomi quando si usa New-Object
. La stessa cosa accade quando si usa un nome di tipo tra parentesi quadre, quindi è possibile specificare [System.Environment] come [Ambiente].
La classe System.Environment contiene informazioni generali sull'ambiente di lavoro per il processo corrente, che si verifica powershell.exe
quando si lavora in Windows PowerShell.
Se si tenta di visualizzare i dettagli di questa classe digitando [System.Environment] | Get-Member, il tipo di oggetto viene segnalato come System.RuntimeType , non System.Environment:
[System.Environment] | Get-Member
TypeName: System.RuntimeType
Per visualizzare i membri statici con Get-Member, specificare il parametro Static:
[System.Environment] | Get-Member -Static
TypeName: System.Environment
Name MemberType Definition
---- ---------- ----------
Equals Method static System.Boolean Equals(Object ob...
Exit Method static System.Void Exit(Int32 exitCode)
...
CommandLine Property static System.String CommandLine {get;}
CurrentDirectory Property static System.String CurrentDirectory ...
ExitCode Property static System.Int32 ExitCode {get;set;}
HasShutdownStarted Property static System.Boolean HasShutdownStart...
MachineName Property static System.String MachineName {get;}
NewLine Property static System.String NewLine {get;}
OSVersion Property static System.OperatingSystem OSVersio...
ProcessorCount Property static System.Int32 ProcessorCount {get;}
StackTrace Property static System.String StackTrace {get;}
SystemDirectory Property static System.String SystemDirectory {...
TickCount Property static System.Int32 TickCount {get;}
UserDomainName Property static System.String UserDomainName {g...
UserInteractive Property static System.Boolean UserInteractive ...
UserName Property static System.String UserName {get;}
Version Property static System.Version Version {get;}
WorkingSet Property static System.Int64 WorkingSet {get;}
TickCount ExitCode
A questo punto è possibile selezionare le proprietà da visualizzare da System.Environment.
Visualizzazione di proprietà statiche di System.Environment
Anche le proprietà di System.Environment sono statiche e devono essere specificate in modo diverso rispetto alle proprietà normali. Viene usato ::
per indicare a Windows PowerShell che si vuole usare un metodo o una proprietà statica. Per visualizzare il comando usato per avviare Windows PowerShell, controlliamo la proprietà CommandLine digitando:
[System.Environment]::Commandline
"C:\Program Files\Windows PowerShell\v1.0\powershell.exe"
Per controllare la versione del sistema operativo, visualizzare la proprietà OSVersion digitando:
[System.Environment]::OSVersion
Platform ServicePack Version VersionString
-------- ----------- ------- -------------
Win32NT Service Pack 2 5.1.2600.131072 Microsoft Windows...
Si può controllare se il computer sta eseguendo l'arresto visualizzando la proprietà HasShutdownStarted:
[System.Environment]::HasShutdownStarted
False
Fare matematica con System.Math
La classe statica System.Math è utile per eseguire alcune operazioni matematiche. La classe include diversi metodi utili, che è possibile visualizzare usando Get-Member
.
Nota
System.Math ha diversi metodi con lo stesso nome, ma sono distinti dal tipo dei relativi parametri.
Digitare il comando seguente per elencare i metodi della classe System.Math.
[System.Math] | Get-Member -Static -MemberType Methods
TypeName: System.Math
Name MemberType Definition
---- ---------- ----------
Abs Method static System.Single Abs(Single value), static Sy...
Acos Method static System.Double Acos(Double d)
Asin Method static System.Double Asin(Double d)
Atan Method static System.Double Atan(Double d)
Atan2 Method static System.Double Atan2(Double y, Double x)
BigMul Method static System.Int64 BigMul(Int32 a, Int32 b)
Ceiling Method static System.Double Ceiling(Double a), static Sy...
Cos Method static System.Double Cos(Double d)
Cosh Method static System.Double Cosh(Double value)
DivRem Method static System.Int32 DivRem(Int32 a, Int32 b, Int3...
Equals Method static System.Boolean Equals(Object objA, Object ...
Exp Method static System.Double Exp(Double d)
Floor Method static System.Double Floor(Double d), static Syst...
IEEERemainder Method static System.Double IEEERemainder(Double x, Doub...
Log Method static System.Double Log(Double d), static System...
Log10 Method static System.Double Log10(Double d)
Max Method static System.SByte Max(SByte val1, SByte val2), ...
Min Method static System.SByte Min(SByte val1, SByte val2), ...
Pow Method static System.Double Pow(Double x, Double y)
ReferenceEquals Method static System.Boolean ReferenceEquals(Object objA...
Round Method static System.Double Round(Double a), static Syst...
Sign Method static System.Int32 Sign(SByte value), static Sys...
Sin Method static System.Double Sin(Double a)
Sinh Method static System.Double Sinh(Double value)
Sqrt Method static System.Double Sqrt(Double d)
Tan Method static System.Double Tan(Double a)
Tanh Method static System.Double Tanh(Double value)
Truncate Method static System.Decimal Truncate(Decimal d), static...
Il comando visualizza diversi metodi matematici. Ecco un elenco di comandi che illustra il funzionamento di alcuni metodi comuni:
[System.Math]::Sqrt(9)
3
[System.Math]::Pow(2,3)
8
[System.Math]::Floor(3.3)
3
[System.Math]::Floor(-3.3)
-4
[System.Math]::Ceiling(3.3)
4
[System.Math]::Ceiling(-3.3)
-3
[System.Math]::Max(2,7)
7
[System.Math]::Min(2,7)
2
[System.Math]::Truncate(9.3)
9
[System.Math]::Truncate(-9.3)
-9