Statik Sınıflar ve Yöntemler Kullanma

tüm .NET Framework sınıfları kullanılarak oluşturulamaz New-Object . Örneğin, ile bir System. Environment veya System. Math nesnesi oluşturmayı denerseniz New-Object , aşağıdaki hata iletilerini alırsınız:

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

Bu hatalar, bu sınıflardan yeni bir nesne oluşturmanın bir yolu olmadığı için oluşur. Bu sınıflar, durum değiştirolmayan yöntemlerin ve özelliklerin başvuru kitaplıklarıdır. Onları oluşturmanız gerekmez, bunları kullanmanız yeterlidir. Bunlar gibi sınıflar ve metotlar statik sınıflar olarak adlandırılır çünkü oluşturulmazlar, yok edilmez veya değiştirilmez. Bunu açık hale getirmek için statik sınıflar kullanan örnekler sunuyoruz.

System. Environment ile ortam verileri alma

genellikle Windows PowerShell bir nesne ile çalışmanın ilk adımı, içerdiği üyeleri bulmak için Get-Member kullanmaktır. Statik sınıflar ile, gerçek sınıf bir nesne olmadığından işlem biraz farklıdır.

Statik System. Environment sınıfına başvurma

Sınıf adını köşeli ayraç ile çevreleyerek statik bir sınıfa başvurabilirsiniz. Örneğin, parantez içinde adı yazarak System. Environment öğesine başvurabilirsiniz. Bunun yapılması bazı genel tür bilgilerini görüntüler:

[System.Environment]
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    Environment                              System.Object

Not

daha önce bahsedildiği gibi, Windows PowerShell otomatik olarak 'System.' öğesini sona erdirir. kullanırken ad yazmak için New-Object . Aynı şey, parantez içine alınmış bir tür adı kullanırken oluşur; bu nedenle [ System. Environment] öğesini [ ortam olarak belirtebilirsiniz.

System. Environment sınıfı, Windows PowerShell içinde çalışırken geçerli işlemin çalışma ortamı hakkında genel bilgiler içerir powershell.exe .

System. Environment yazarak bu sınıfın ayrıntılarını görüntülemeye çalışırsanız [ | Get-member, nesne türünün System . Environment değil System. RuntimeType olduğu bildirilir:

[System.Environment] | Get-Member
   TypeName: System.RuntimeType

Get-member ile statik üyeleri görüntülemek için statik parametresini belirtin:

[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

Artık System. Environment içinden görüntülenecek özellikleri seçebilirsiniz.

System. Environment 'ın statik özelliklerini görüntüleme

System. Environment özellikleri de statiktir ve normal özelliklerden farklı bir biçimde belirtilmelidir. ::statik bir yöntem veya özellik ile çalışmak istediğimiz Windows PowerShell göstermek için kullanırız. Windows PowerShell başlatmak için kullanılan komutu görmek için CommandLine özelliğini şunu yazarak denetliyoruz:

[System.Environment]::Commandline
"C:\Program Files\Windows PowerShell\v1.0\powershell.exe"

İşletim sistemi sürümünü denetlemek için, şunu yazarak OSVersion özelliğini görüntüleyin:

[System.Environment]::OSVersion
           Platform ServicePack         Version             VersionString
           -------- -----------         -------             -------------
            Win32NT Service Pack 2      5.1.2600.131072     Microsoft Windows...

HasShutdownStarted özelliğini görüntüleyerek bilgisayarın kapanma sürecinde olup olmadığını kontrol edebilirsiniz:

[System.Environment]::HasShutdownStarted
False

System. Math ile matematik yapma

System. Math statik sınıfı bazı matematiksel işlemleri gerçekleştirmek için yararlıdır. System. Math ' in önemli üyeleri genellikle, kullanarak görüntülenebilecek yöntemlerdir Get-Member .

Not

System. Math aynı ada sahip çeşitli yöntemlere sahiptir, ancak parametrelerinin türüne göre ayırt edilir.

System. Math sınıfının yöntemlerini listelemek için aşağıdaki komutu yazın.

[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...

Bu, birkaç matematik yöntemi görüntüler. Ortak yöntemlerin bazılarının nasıl çalıştığını gösteren komutların listesi aşağıda verilmiştir:

[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